#include <iostream> #include <opencv2/opencv.hpp> #include <iostream> #include <opencv2/core.hpp> #include <opencv2/highgui.hpp> using namespace cv; using namespace std; int main() { Mat src = Mat::zeros(Size(400,500),CV_8UC3);
src = Scalar(255,0,0);
imshow("输出窗口", src);
waitKey(0);
destroyAllWindows();
cout << "Hello World!\n";
return 0;
}
#include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main() { Mat src = imread("D:/opencv/opencvtest/1.png"); Mat gray; namedWindow("input", WINDOW_AUTOSIZE); cvtColor(src,gray,COLOR_BGR2GRAY);//第二个参数必须很重要如果写错的话这个代码就会报错。 imshow("mat",gray); waitKey(0); destroyAllWindows(); cout << "Hello World!\n"; return 0; }
1 #include <iostream> 2 #include <opencv2/opencv.hpp> 3 #include <iostream> 4 #include <opencv2/core.hpp> 5 #include <opencv2/highgui.hpp> 6 7 using namespace cv; 8 using namespace std; 9 10 int main() 11 { 12 Mat src = Mat::zeros(Size(400,500),CV_8UC3);//创建一个400*500的3通道全是0的图像 13 //cout << "width:"<<src.cols << "height:" << src.rows <<"channels:"<<src.channels() << endl;//可以打印这个宽高通道数。 14 //cout<<src<<endl;//打印一下 发现有很多的0打印,可以把上面的size变小一点打印。 15 //还有一个创建空白图像的方法就是 16 //Mat src = Mat::ones(Size(400,500),CV_8UC1); //创建一个400*500的1通道全为1的图像但是这里只对一通道有效。 17 18 //Mat src = Mat::ones(Size(400,500),CV_8UC3) //创建400*500 的3通道 1,0,0,为一组的数据。 //src = 127; 这个式子只会对三通道里面的B通道赋值,其他的还是原有的值,想要3个通道都赋值的话要用Scalar函数。 19 src = Scalar(255,0,0); //使用Scalar去填充图像里面的数据。 BGR。 20 imshow("输出窗口", src); 21 22 //Mat m1; 赋值 23 //m1 = src; 这个m1类似于一个指针,我们修改m4的值,src的值也会对应的改变。 24 //m1.Scalar(0,0,255); 25 26 //下面的2种都是创建一个新的图像 即使修改也不会对src产生影响。 27 28 //Mat m2; 复制 29 //src.copyTo(m2); 30 //Scalar(0,0,255); 31 32 //Mat m3; 克隆 33 //m3=src.clone(); 34 //Scalar(0,0,255); 35 waitKey(0); 36 destroyAllWindows(); 37 cout << "Hello World!\n"; 38 39 return 0; 40 }
4.1三通道
#include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main() { Mat src = Mat::zeros(Size(100, 100), CV_8UC3);//这里设置的很大下面不要用io去读取,很很久很久 src = Scalar(255, 0, 0); int height = src.rows; int width = src.cols; int channel = src.channels(); cout << "height" << height << "width" << width << "channel"<<channel << endl; cout << src << endl; for (int i = 0;i < height;i++) { for (int j = 0;j < width;j++) {//我们这里是三通道的值 Vec3b bgr = src.at<Vec3b>(i, j); //读取数据并对他进行修改 bgr[0] = 111; bgr[1] = 113; bgr[2] = 114; src.at<Vec3b>(i, j)=bgr;
} } cout << "------------------------------" << endl; cout << src << endl; imshow("mat",src); waitKey(0); destroyAllWindows(); cout << "Hello World!\n"; return 0; }
4.2 一通道
#include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main() { Mat src = Mat::zeros(Size(100, 100), CV_8UC1); src = 12; int height = src.rows; int width = src.cols; int channel = src.channels(); cout << "height" << height << "width" << width << "channel"<<channel << endl; cout << src << endl; for (int i = 0;i < height;i++) { for (int j = 0;j < width;j++) {//我们这里是一通道的值 int data = src.at<uchar>(i, j);//读 src.at<uchar>(i, j) = 255 - data;//写 } } cout << "------------------------------" << endl; cout << src << endl; imshow("mat",src); waitKey(0); destroyAllWindows(); cout << "Hello World!\n"; return 0; }
4.3 指针的方法
#include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main() { Mat src = Mat::zeros(Size(100, 100), CV_8UC1); src = 12; int height = src.rows; int width = src.cols; int channel = src.channels(); cout << "height" << height << "width" << width << "channel"<<channel << endl; cout << src << endl; for (int i = 0;i < height;i++) { uchar* point = src.ptr<uchar>(i); for (int j = 0;j < width;j++) {//我们这里是一通道的值 int data = *point;//读 *point++ = 255 - data;//写 } } cout << "------------------------------" << endl; cout << src << endl; imshow("mat",src); waitKey(0); destroyAllWindows(); cout << "Hello World!\n"; return 0; }
#include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main() { Mat src = Mat::zeros(Size(100, 100), CV_8UC3);//这里设置的很大下面不要用io去读取,很很久很久 src = Scalar(255, 12, 0); int height = src.rows; int width = src.cols; int channel = src.channels(); cout << "height" << height << "width" << width << "channel" << channel << endl; cout << src << endl; for (int i = 0;i < height;i++) { uchar* point = src.ptr<uchar>(i); for (int j = 0;j < width;j++) {//我们这里是三通道的值 *point++ = 255 - *point; *point++ = 255 - *point; *point++ = 255 - *point; } } cout << "------------------------------" << endl; cout << src << endl; imshow("mat", src); waitKey(0); destroyAllWindows(); cout << "Hello World!\n"; return 0; }
原文:https://www.cnblogs.com/yepc/p/14496198.html