opencv::調整圖像亮度與對比度

 

 

圖像變換能夠看做以下: - 像素變換 – 點操做 - 鄰域操做 – 區域 調整圖像亮度和對比度屬於像素變換-點操做

 

//建立一張跟原圖像大小和類型一致的空白圖像、像素值初始化爲0
Mat new_image = Mat::zeros( image.size(), image.type() ); //確保值大小範圍爲0~255之間
saturate_cast<uchar>(value) //給每一個像素點每一個通道賦值
Mat.at<Vec3b>(y,x)[index]=value

 

int main(int argc, char** argv) { Mat src, dst; src = imread(STRPAHT2); if (!src.data) { printf("could not load image...\n"); return -1; } char input_win[] = "input image"; cvtColor(src, src, CV_RGB2GRAY); namedWindow(input_win, CV_WINDOW_AUTOSIZE); imshow(input_win, src); int height = src.rows; int width = src.cols; //建立一張跟原圖像大小和類型一致的空白圖像、像素值初始化爲0
    dst = Mat::zeros(src.size(), src.type()); float alpha = 1.2; float beta = 30; Mat m1; src.convertTo(m1, CV_32F); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { if (src.channels() == 3) { float b = m1.at<Vec3f>(row, col)[0];// blue
                float g = m1.at<Vec3f>(row, col)[1]; // green
                float r = m1.at<Vec3f>(row, col)[2]; // red // output
                dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b*alpha + beta); dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g*alpha + beta); dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r*alpha + beta); } else if (src.channels() == 1) { float v = src.at<uchar>(row, col); dst.at<uchar>(row, col) = saturate_cast<uchar>(v*alpha + beta); } } } char output_title[] = "contrast and brightness change demo"; namedWindow(output_title, CV_WINDOW_AUTOSIZE); imshow(output_title, dst); waitKey(0); return 0; }
相關文章
相關標籤/搜索