Opencv3 C ++ VS2017スタディノート05画像の明るさとコントラストを調整する

 画像の明るさとコントラストを調整する(ピクセルレベルの操作)


  • 近隣事業:エリア
    • 特徴抽出
    • 画像の識別
    • 検出およびその他のアプリケーション
  • ポイント操作:ピクセル変換
    • \ large \ large g \ left(i、j \ right)= \ alpha f(ij)+ \ beta、\ alpha> 0、\ betaゲイン変数は
    • 明るさはピクセル値であり、値が大きいほど明るくなります
    • コントラストは違いです
  • 一般的に使用される関連API
    • Mat dst = Mat :: zeros(src.size()、src.type()):
      • srcと同じサイズとタイプのMatオブジェクトdstを作成します
    • saturate_cast <uchar>(値)
      • ピクセル値が0〜255であることを確認してください
      • はいの場合は値を返し、いいえの場合は0を返します
    • Mat.at <Vec3b>(y、x)[インデックス]   
      • ピクセルのインデックスチャネルの値を取得します
      • Mat.at <Vec3b>(y、x)[index] =値の割り当て
#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>

using namespace std;


int main(int argc, char ** argv)
{
	using namespace cv;
	Mat src0, src1;
	src0 = imread("C:\\Users\\xujin\\Desktop\\test0.JPG");
	if (!src0.data)
	{
		cout << "no image";
		return -1;
	}
	namedWindow("src0_image", WINDOW_AUTOSIZE);
	imshow("src0_image", src0);
	src1 = imread("C:\\Users\\xujin\\Desktop\\test1.JPG");
	if (!src1.data)
	{
		cout << "no image";
		return -1;
	}
	namedWindow("src1_image", WINDOW_AUTOSIZE);
	imshow("src1_image", src1);


	//调整亮度

	Mat dst = src0.clone();
	namedWindow("dst_image", WINDOW_AUTOSIZE);
	imshow("dst_image", dst);

	//对dst对象进行像素级操作
	int height = dst.rows;
	int width = dst.cols;
	float alpha = 1.2;
	float beta = 123;
	
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (dst.channels() == 3)
			{
				float b = dst.at<Vec3b>(row, col)[0];    //如果图片不是3f的数据,则必然error,所以要先转换成3f数据
				float g = dst.at<Vec3b>(row, col)[1];
				float r = dst.at<Vec3b>(row, col)[2];
				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 (dst.channels() == 1)
			{
				float piexl = dst.at<uchar>(row, col);
				dst.at<Vec3f>(row, col) = saturate_cast<uchar>(piexl*alpha + beta);
			}
			else
			{
				cout << "channels error" << endl;
				return -1;
			}
		}
	}

	string output = "output";
	namedWindow(output, WINDOW_AUTOSIZE);
	imshow(output, dst);
	waitKey(0);
	return 0;
}

 

おすすめ

転載: blog.csdn.net/Mrsherlock_/article/details/104492365