gpt4 book ai didi

c++ - 改变图像部分的亮度和对比度

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:59:58 25 4
gpt4 key购买 nike

给定一张图片,我想更改图片部分的亮度/对比度。

我正在使用示例 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 步:裁剪图像中要更改对比度的部分。

第 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)));

希望对您有所帮助!

关于c++ - 改变图像部分的亮度和对比度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43853560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com