您好,登錄后才能下訂單哦!
最近在學(xué)習(xí)opencv圖像處理,自學(xué)到將一副原圖像上的像素點(diǎn)像素值反轉(zhuǎn),再輸出新的圖像
,代碼如下:
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
Mat gray_image;
Mat src = imread("C:/Users/Administrator/Desktop/1.jpg");
if (!src.data)
{
cout << "could not load image..." << endl;
return -1;
}
cvNamedWindow("原圖",WINDOW_AUTOSIZE);
imshow("原圖", src);
cvtColor(src, gray_image, CV_BGR2GRAY);
//namedWindow("output", CV_WINDOW_AUTOSIZE);
//imshow("output", gray_image);
//int height = gray_image.rows;
//int width = gray_image.cols;
Mat dst;
dst.create(src.size(), src.type());
int height = src.rows;
int 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_image.at<uchar>(row, col);
gray_image.at<uchar>(row, col) = 255 - gray;
}
//三通道
else if (nc == 3) {
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] = 255-b;
dst.at<Vec3b>(row, col)[1] = 255 - g;
dst.at<Vec3b>(row, col)[2] = 255 - r;
gray_image.at<uchar>(row, col) = max(r, max(b, g));//取最大值作為灰度值
}
}
}
imshow("output", gray_image);
//bitwise_not(src, dst);
imshow("gray invert", dst);
waitKey(0);
return 0;
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。