Location>code7788 >text

C++'s OpenCV Beginner to Improver 005: 005 Image Manipulation

Popularity:874 ℃/2024-11-15 11:04:34
1 #include <opencv2/> 2 #include <iostream> 3 #include <> 4 5 using namespace std; 6 using namespace cv; 7 8 /// <summary> 9 /// Image manipulation 10 /// 1、Read and write images 11 /// 2、Read and write pixels 12 /// 3、Modify the pixel value 13 /// </summary> 14 /// <returns></returns> 15 int main() 16 { 17 //1、Read and write images 18 //1.1. imread can be specified to load grayscale or RGB images. 19 //1.2. imwrite can save images, the type is determined by the extension. 20 Mat src; 21 src = imread("D:\\360MoveData\\Users\\Administrator\\Desktop\\TestImage\\", IMREAD_UNCHANGED); 22 if (()) 23 { 24 cout << "The image failed to load!!!" << endl; 25 return -1; 26 } 27 28 namedWindow("original figure", WINDOW_AUTOSIZE); 29 imshow("original figure", src); 30 31 //2、Read and write pixels 32 //2.1. Pixel value of all gray (Gray) pixels (CV_8UC1) 33 // Scalar intensity=<uchar>(row,col); 34 // Scalar intensity=<uchar>(Point(row,col)); 35 // 36 //2.2. Read the pixel value of a color (RGB) pixel point. 37 // Vec3f intensity=<Vec3f>(row,col); 38 // float blue=[0]; 39 // float green=[1]; 40 // float red=[2]; 41 // 42 // Vec3f is RGB data of float type. 43 // 44 //2.3 Modify single-channel grayscale pixel values 45 // <uchar>(row,col)=128; 46 // 47 // 2.4 Modify RGB three-channel pixel value 48 // <Vec3b>(row,col)[0]=128; 49 // <Vec3b>(row,col)[1]=128; 50 // <Vec3b>(row,col)[2]=128; 51 // 52 // 2.5. Blank Pixel Assignment 53 // src=Scalar(0); 54 // 55 // 56 // 2.6. Vec3b and Vec3f 57 // 2.6.1. Vec3b corresponds to the uchar type data of blue, green, and red in the order of the three channels. 58 // 2.6.2. Vec3f corresponds to the float type data of the three channels in the order of blue, green and red. 59 // 2.6.3. Convert CV_8UC1 to CV32F1 as follows: (dst,CV_32F). 60 // 61 //2.1, read single-channel pixel value, sample code: 62 Mat graySrc; 63 cvtColor(src, graySrc, COLOR_BGR2GRAY);//Converts color RGB 3-channel images to grayscale single-channel images. 64 namedWindow("Single-channel grayscale image", WINDOW_AUTOSIZE); 65 imshow("Single-channel grayscale image", graySrc); 66 67 int width = ; 68 int height = ; 69 70 for (int row = 0; row < height; row++) 71 { 72 for (int col = 0; col < width; col++) 73 { 74 //Read single-channel, grayscale pixel values 75 int gray = <uchar>(row, col); 76 //Modify pixel values for single channel, grayscale 77 <uchar>(row, col) = 255 - gray; 78 } 79 } 80 81 imshow("Modified single-channel grayscale image", graySrc);//You can use the display image directly and he will automatically create the window to display the picture. 82 83 //Handles multi-channel, color images 84 width = ; 85 height = ; 86 int channes = (); 87 88 Mat dst; 89 ((),()); 90 91 for (int row = 0; row < height; row++) 92 { 93 for (int col = 0; col < width; col++) 94 { 95 if (channes == 1) 96 { 97 //Read single-channel, grayscale pixel values 98 int gray = <uchar>(row, col); 99 //Modify pixel values for single channel, grayscale 100 <uchar>(row, col) = 255 - gray; 101 } 102 else 103 { 104 //Reads multi-channel, color pixel values. vec3b is the data structure of a BGR with 3 channels. 105 Vec3b myvalue=<Vec3b>(row, col); 106 int b = [0]; 107 int g = [1]; 108 int r = [2]; 109 110 //Modify multi-channel, color pixel values, which is the opposite effect. 111 /*<Vec3b>(row, col)[0] = 255 - b; 112 <Vec3b>(row, col)[1] = 255 - g; 113 <Vec3b>(row, col)[2] = 255 - r;*/ 114 115 //Modify multi-channel, colored pixel values, which are only blue and green, cyan is the color. 116 /*<Vec3b>(row, col)[0] = b; 117 <Vec3b>(row, col)[1] = g; 118 <Vec3b>(row, col)[2] = 0;*/ 119 120 //Modify multi-channel, color pixel values, which are only blue and red, that is, purple. 121 /*<Vec3b>(row, col)[0] = b; 122 <Vec3b>(row, col)[1] = 0; 123 <Vec3b>(row, col)[2] = r;*/ 124 125 //Modify the multi-channel, colored pixel values, which are only green and red, that is, yellow. 126 <Vec3b>(row, col)[0] = 0; 127 <Vec3b>(row, col)[1] = g; 128 <Vec3b>(row, col)[2] = r; 129 130 //<uchar>(row, col) = min(r,min(b,g)); take gray second method 131 } 132 } 133 } 134 135 imshow("Modified single and multi-channel images", dst);//You can use the display image directly and he will automatically create the window to display the picture. 136 //imshow("Single-channel grayscale image", graySrc); 137 //The opencv interface can also achieve this contrasting effect 138 bitwise_not(src, dst); 139 140 imshow("bitwise_not Modified single and multi-channel images", dst);//You can use the display image directly and he will automatically create the window to display the picture. 141 142 143 waitKey(0); 144 145 system("pause"); 146 return 0; 147 }