Draw a color bar 10 in the first row of the original image
class Program { static void Main(String[] args) { Mat img = CvInvoke.Imread(@"C:\Users\dell\Pictures\mach.jpg"); CvInvoke.Imshow("draw", img); Mat dst = img.Clone(); int row = img.Rows; int col = img.Cols * img.NumberOfChannels; Console.WriteLine("DepthType is " + dst.Depth+"and elementSize is"+dst.ElementSize); for (int i = 0; i < 20; i++) for (int j = 0; j < img.Cols;j++) { SetAtB(dst, i, j, 0); SetAtG(dst, i, j, 255); SetAtR(dst, i, j, 0); } CvInvoke.Imshow("hello", dst); CvInvoke.WaitKey(0); } private static IntPtr GetAt(Mat mat,int row,int col) { return mat.DataPointer + (row * mat.Cols + col) * mat.ElementSize; } private static void SetAtB(Mat mat, int row, int col, byte value) { Marshal.Copy(new byte[] { value }, 0, GetAt(mat, row, col), 1); } private static void SetAtG(Mat mat, int row, int col, byte value) { Marshal.Copy(new byte[] { value }, 0, GetAt(mat, row, col)+1, 1); } private static void SetAtR(Mat mat, int row, int col, byte value) { Marshal.Copy(new byte[] { value }, 0, GetAt(mat, row, col)+2, 1); } }
Similar requirements two methods in C ++
method 1
#include <opencv2 / opencv.hpp> #include <the iostream> the using namespace CV; the using namespace STD; void main () { // Dynamic Address access Mat imread IMG = ( " E: //green.png " ); imshow ( " the src " , IMG); Mat DST = img.clone (); int rowNumber = img.rows; // get the number of rows int colNumber = img.cols; // Get the number of columns for ( int I = 0 ; I <rowNumber ; I ++ ) { for( Int J = 0 ; J <colNumber; J ++ ) { dst.at <Vec3b> (I, J) [ 0 ] = 255 ; // blue channel dst.at <Vec3b> (I, J) [ . 1 ] = 0 ; // green channel dst.at <Vec3b> (I, J) [ 2 ] = 0 ; // red channel // dst.at <UCHAR> (I, J) = 255; // gray image } } imshow ( " DST " , DST); waitKey ( 0 ); }
Method 2:
#include <opencv2 / opencv.hpp> #include <the iostream> the using namespace CV; the using namespace STD; void main () { Mat IMG = imread ( " E: //green.png " ); imshow ( " the src " , IMG ); Mat DST = img.clone (); int rowNumber = img.rows; // get the number of rows int colNumber = img.cols img.channels * (); // number of columns in each row × number of channel elements = for ( int I = 0 ; I <rowNumber; I ++ ) { UCHARData = dst.ptr * <UCHAR> (I); // get the first address of each row, ptr address of the image function can be any of the first row for ( int J = 0 ; J <colNumber; J ++ ) { // Data [J ] = 255; // grayscale Switch (J% . 3 ) { Case 0 : // blue channel Data [J] = 255 ; BREAK ; Case . 1 : // green channel Data [J] = 0 ; BREAK ; Case 2 :// red channel Data [J] = 255 ; BREAK ; } } } imshow ( " DST " , DST); waitKey ( 0 ); }