2017-05-08 102 views
4

給定圖像,我想更改圖像部分的亮度/對比度。更改圖像部分的亮度和對比度

我用一個例子here改變整個圖像的亮度/對比度:

RNG rng(cv::getTickCount()); 
float min_alpha = 0.1; 
float max_alpha = 2.0; 
float alpha = rng.uniform(min_alpha, max_alpha); 
float beta = -2.0; 
image.convertTo(new_image, -1, alpha, beta); 

有沒有辦法做到這一點只在圖像的局部區域,而不必通過整個迭代一個for循環的圖像?

回答

1

可以在一個更容易/更有效的方式通過以下步驟做到這一點:

第1步:要改變對比度的圖像裁剪部分。

第2步:對裁剪後的圖像應用合適的對比度/亮度變化。

第3步:粘貼更改圖像回到原始圖像。

// Step 1 
int rect_x = originalImg.cols/5; 
int rect_y = 0; 
int rect_width = originalImg.cols/6; 
int rect_height = originalImg.rows; 

cv::Rect ROI(rect_x, rect_y, rect_width, rect_height); 
cv::Mat cropped_image = originalImg (ROI); 

Mat image = imread(argv[1]); 
Mat new_image = Mat::zeros(image.size(), image.type()); 

// Step 2 
std::cout<<" Basic Linear Transforms "<<std::endl; 
std::cout<<"-------------------------"<<std::endl; 
std::cout<<"* Enter the alpha value [1.0-3.0]: ";std::cin>>alpha; 
std::cout<<"* Enter the beta value [0-100]: "; std::cin>>beta; 

for(int y = 0; y < cropped_image.rows; y++) { 
    for(int x = 0; x < cropped_image.cols; x++) { 
     for(int c = 0; c < 3; c++) { 
      enhanced_cropped_image.at<Vec3b>(y,x)[c] = 
      saturate_cast<uchar>(alpha*(cropped_image.at<Vec3b>(y,x)[c]) + beta); 
     } 
    } 
} 
// Or this for Step 2 
float min_alpha = 0.1; 
float max_alpha = 2.0; 
float alpha = rng.uniform(min_alpha, max_alpha); 
float beta = -2.0; 
cropped_image.convertTo(enhanced_cropped_image, -1, alpha, beta); 
// Step 3 
enhanced_cropped_image.copyTo(originalImg(cv::Rect(rect_x, rect_y, rect_width, rect_height))); 

希望它有幫助!

1

以下是一種方法。

  1. 改變整個圖像

  2. 的亮度/對比度指定區域與CV ::矩形要裁剪

  3. 複製裁剪部分相同的位置在原圖像

這裏有一個片段我寫道:

Mat partial_illum_img; 
RNG rng(cv::getTickCount()); 
float min_alpha = 0.1; 
float max_alpha = 2.0; 
float alpha = rng.uniform(min_alpha, max_alpha); 
float beta = -2.0; 
image.convertTo(illum_img, -1, alpha, beta); 
image.copyTo(partial_illum_img); 

// Crop a rectangle from converted image 
int rect_x = illum_img.cols/5; 
int rect_y = 0; 
int rect_width = illum_img.cols/6; 
int rect_height = illum_img.rows; 

cv::Rect illumROI(rect_x, rect_y, rect_width, rect_height); 
cv::Mat crop_after_illum = illum_img(illumROI); 
crop_after_illum.copyTo(partial_illum_img(cv::Rect(rect_x, rect_y, rect_width, rect_height))); 
+0

可以爲圖像,我不認爲有需要,當你想它只是其中的一部分 –

+0

@RickM改變整個圖像的亮度/對比度的一部分,這樣做。你的意思是我可以在圖像上指定Rect並改變其對比度? – MoneyBall

+0

是的。您可以從要更改對比度的圖像中提取Rect,然後將其粘貼回原始圖像。 –

相關問題