可以在一個更容易/更有效的方式通過以下步驟做到這一點:
第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)));
希望它有幫助!
可以爲圖像,我不認爲有需要,當你想它只是其中的一部分 –
@RickM改變整個圖像的亮度/對比度的一部分,這樣做。你的意思是我可以在圖像上指定Rect並改變其對比度? – MoneyBall
是的。您可以從要更改對比度的圖像中提取Rect,然後將其粘貼回原始圖像。 –