You can do this in a simpler and more efficient way with the following steps:
Step 1: Crop the part of the image where you want to change the contrast.
Step 2: Apply the appropriate contrast / brightness changes to this cropped image.
Step 3: Paste the modified image back into the original image.
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() );
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 );
}
}
}
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);
enhanced_cropped_image.copyTo(originalImg(cv::Rect(rect_x, rect_y, rect_width, rect_height)));
, !