OpenCV Mat image pixel value reading and writing

Since the 16-bit single-channel image data in raw format needs to be read in and converted into 8-bit for drawing, the Mat format of opencv is used, so the read image data array needs to be assigned to Mat, and then imshowed, but It is necessary to draw a colored rectangular frame on the image at the same time, so it needs to be assigned to the three-channel Mat, so the assignment and reading of the pixel value of the Mat are searched.
The main syntax used is to access the pixel value of opencv Mat:

Mat image;
image.at<存储类型名称>(行,列)[通道]

For single-channel grayscale images, you can use

image.at<uchar>(i, j)

For RGB images you can use

image.at<Vec3b>(i, j)[0]  
image.at<Vec3b>(i, j)[1]  
image.at<Vec3b>(i, j)[2]

1. Assign the array to gray’s Mat

unsigned short array0[IMGSIZE];// = imread("office.jpg",0);
	Mat read0(ROW, COL, CV_8UC1, Scalar(100));
	uchar* pxvec;

	for (i = 0; i < ROW; i++)
	{
    
    
		for (j = 0; j < COL; j++)
		{
    
    
			*(array0 + i * COL + j) = i + j;
		}
	}
	//数值复制给Mat
	for (i = 0; i < ROW; i++)
	{
    
    
		pxvec = read0.ptr<uchar>(i);
		for (j = 0; j < COL; j++)
		{
    
    
			pxvec[j] = *(array0 + i*COL + j) / 2;
			//read.at<uchar>(i,j) = *(img11 + i*COL + j)/2;
		}
	}
	cv::rectangle(read0, cv::Point(100, 50), cv::Point(100 + 20 - 1, 50 + 20 - 1), cv::Scalar(0, 255, 0), 2, 8, 0);
	namedWindow("fig", CV_WINDOW_AUTOSIZE);
	imshow("fig", read0);
	waitKey(0);

Insert image description here

2. Copy the RGB image to the RGB Mat

Figures and frames are in color

Mat read = imread("office.jpg");
	Mat img(read.rows, read.cols, CV_8UC3, Scalar(0, 255, 0));
	for (int j = 0; j < read.rows; j++)
	{
    
    
		for (int k = 0; k < read.cols; k++)
		{
    
    
			img.at<Vec3b>(j, k)[0] = read.at<Vec3b>(j, k)[0];
			img.at<Vec3b>(j, k)[1] = read.at<Vec3b>(j, k)[1];
			img.at<Vec3b>(j, k)[2] = read.at<Vec3b>(j, k)[2];
		}
	}
	cv::rectangle(img, cv::Point(100, 50), cv::Point(100 + 20 - 1, 50 + 20 - 1), cv::Scalar(0, 255, 0), 2, 8, 0);
	namedWindow("fig", CV_WINDOW_AUTOSIZE);
	imshow("fig", img);
	waitKey(0);

Insert image description here

3.Copy the Gray image to the RGB Mat

The images of the three channels are the same

Mat read_gray = imread("office.jpg", IMREAD_GRAYSCALE);
	Mat img_rbg(read_gray.rows, read_gray.cols, CV_8UC3, Scalar(0, 255, 0));
	for (int j = 0; j < read_gray.rows; j++)
	{
    
    
		for (int k = 0; k < read_gray.cols; k++)
		{
    
    
			img_rbg.at<Vec3b>(j, k)[0] = read_gray.at<uchar>(j, k);
			img_rbg.at<Vec3b>(j, k)[1] = read_gray.at<uchar>(j, k);
			img_rbg.at<Vec3b>(j, k)[2] = read_gray.at<uchar>(j, k);
		}
	}
	cv::rectangle(img_rbg, cv::Point(100, 50), cv::Point(100 + 20 - 1, 50 + 20 - 1), cv::Scalar(0, 255, 0), 2, 8, 0);
	namedWindow("fig", CV_WINDOW_AUTOSIZE);
	imshow("fig", img_rbg);
	waitKey(0);

Insert image description here

4.Copy the Gray image to gray’s Mat

The drawn rectangular frame is also a colorless gray frame.

Mat img_gray(read_gray.rows, read_gray.cols, CV_8UC1, Scalar(100));
	for (int j = 0; j < read_gray.rows; j++)
	{
    
    
		for (int k = 0; k < read_gray.cols; k++)
		{
    
    
			img_gray.at<uchar>(j, k) = read_gray.at<uchar>(j, k);
			img_gray.at<uchar>(j, k) = read_gray.at<uchar>(j, k);
			img_gray.at<uchar>(j, k) = read_gray.at<uchar>(j, k);
		}
	}
	/*img.ptr<uchar>(0)[0] = 0;
	img.ptr<uchar>(0)[1] = 0;
	img.ptr<uchar>(0)[2] = 255;*/
	cv::rectangle(img_gray, cv::Point(100, 50), cv::Point(100 + 20 - 1, 50 + 20 - 1), cv::Scalar(0, 255, 0), 2, 8, 0);
	namedWindow("fig", CV_WINDOW_AUTOSIZE);
	imshow("fig", img_gray);
	waitKey(0);

Insert image description here

Complete code

#include<iostream>
#include<opencv2\opencv.hpp>
//#include<opencv2\highgui\highgui.hpp>
using namespace std;
using namespace cv;
#define ROW 256
#define COL 256
#define IMGSIZE ROW*COL
int main()
{
    
    
	int i, j;
	

	//1.灰度图像赋值给gray的Mat;
	//数组赋值
unsigned short array0[IMGSIZE];// = imread("office.jpg",0);
	Mat read0(ROW, COL, CV_8UC1, Scalar(100));
	uchar* pxvec;

	for (i = 0; i < ROW; i++)
	{
    
    
		for (j = 0; j < COL; j++)
		{
    
    
			*(array0 + i * COL + j) = i + j;
		}
	}
	//数值复制给Mat
	for (i = 0; i < ROW; i++)
	{
    
    
		pxvec = read0.ptr<uchar>(i);
		for (j = 0; j < COL; j++)
		{
    
    
			pxvec[j] = *(array0 + i*COL + j) / 2;
			//read.at<uchar>(i,j) = *(img11 + i*COL + j)/2;
		}
	}
	cv::rectangle(read0, cv::Point(100, 50), cv::Point(100 + 20 - 1, 50 + 20 - 1), cv::Scalar(0, 255, 0), 2, 8, 0);
	namedWindow("fig", CV_WINDOW_AUTOSIZE);
	imshow("fig", read0);
	waitKey(0);
	//2.RGB图像复制给RGB的Mat
	Mat read = imread("office.jpg");
	Mat img(read.rows, read.cols, CV_8UC3, Scalar(0, 255, 0));
	for (int j = 0; j < read.rows; j++)
	{
    
    
		for (int k = 0; k < read.cols; k++)
		{
    
    
			img.at<Vec3b>(j, k)[0] = read.at<Vec3b>(j, k)[0];
			img.at<Vec3b>(j, k)[1] = read.at<Vec3b>(j, k)[1];
			img.at<Vec3b>(j, k)[2] = read.at<Vec3b>(j, k)[2];
		}
	}
	cv::rectangle(img, cv::Point(100, 50), cv::Point(100 + 20 - 1, 50 + 20 - 1), cv::Scalar(0, 255, 0), 2, 8, 0);
	namedWindow("fig", CV_WINDOW_AUTOSIZE);
	imshow("fig", img);
	waitKey(0);
	//Gray图像复制给RGB的Mat
	Mat read_gray = imread("office.jpg", IMREAD_GRAYSCALE);
	Mat img_rbg(read_gray.rows, read_gray.cols, CV_8UC3, Scalar(0, 255, 0));
	for (int j = 0; j < read_gray.rows; j++)
	{
    
    
		for (int k = 0; k < read_gray.cols; k++)
		{
    
    
			img_rbg.at<Vec3b>(j, k)[0] = read_gray.at<uchar>(j, k);
			img_rbg.at<Vec3b>(j, k)[1] = read_gray.at<uchar>(j, k);
			img_rbg.at<Vec3b>(j, k)[2] = read_gray.at<uchar>(j, k);
		}
	}
	cv::rectangle(img_rbg, cv::Point(100, 50), cv::Point(100 + 20 - 1, 50 + 20 - 1), cv::Scalar(0, 255, 0), 2, 8, 0);
	namedWindow("fig", CV_WINDOW_AUTOSIZE);
	imshow("fig", img_rbg);
	waitKey(0);
	//Gray图像复制给gray的Mat
	Mat img_gray(read_gray.rows, read_gray.cols, CV_8UC1, Scalar(100));
	for (int j = 0; j < read_gray.rows; j++)
	{
    
    
		for (int k = 0; k < read_gray.cols; k++)
		{
    
    
			img_gray.at<uchar>(j, k) = read_gray.at<uchar>(j, k);
			img_gray.at<uchar>(j, k) = read_gray.at<uchar>(j, k);
			img_gray.at<uchar>(j, k) = read_gray.at<uchar>(j, k);
		}
	}
	/*img.ptr<uchar>(0)[0] = 0;
	img.ptr<uchar>(0)[1] = 0;
	img.ptr<uchar>(0)[2] = 255;*/
	cv::rectangle(img_gray, cv::Point(100, 50), cv::Point(100 + 20 - 1, 50 + 20 - 1), cv::Scalar(0, 255, 0), 2, 8, 0);
	namedWindow("fig", CV_WINDOW_AUTOSIZE);
	imshow("fig", img_gray);
	waitKey(0);
	destroyWindow("fig");
	return 0;
}

Guess you like

Origin blog.csdn.net/alansss/article/details/121131324