OpenCV图像操作——修改像素值 |
例子
int main()
{
Mat src,gray_src;
src=imread("图像路径");
if(src.empty())
{
cout<<"could not load image...";
return -1;
}
namedWindow("input",CV_WINDOW_AUTOSIZE);
inshow("input",src);
cvtColor(src,gray_src,CV_BGR2GRAY);
namedWindow("output",CV_WINDOW_AUTOSIZE);
int height=gray_src.rows;
int width=gray_src.cols;
for(int row=0;row<height;row++)
{
for(int col=0;col<width;col++)
{
int gray=gray_src.at<uchar>(row,col);
gray_src.at<uchar>(row,col)=255-gray;
}
}
Mat dst;
dst.create(src.siize(),src.type(());
height=src.rows();
width=src.cols;
int nc=src.channels();
for(int row=0;row<height;row++)
{
for(int col=0;col<width;col++)
{
if(nc==1)
{
int gray=gray_src.at<uchar>(row,col);
gray_src.at<uchar>(row,col)=255-gray;
}
else if(nc==3&&src.type()==16)
{
int b=src.at<Vec3b>(row,col)[0];
int g=src.at<Vec3b>(row,col)[1];
int r=src.at<Vec3b>(row,col)[2];
dst.at<Vec3b>(row,col)[0]=0;
dst.at<Vec3b>(row,col)[1]=g;
dst.at<Vec3b>(row,col)[2]=r;
gray_src.at<uchar>(row,col)=max(r,max(b,g));
}
}
}
//bitwise_not(src,dst);
imshow("output",dst);
imshow("output",gray_src);
waitKey(0);
return 0;
}