1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > opencv遍历像素输出像素值

opencv遍历像素输出像素值

时间:2021-12-24 01:23:25

相关推荐

opencv遍历像素输出像素值

#include<iostream>#include<opencv2/core/core.hpp>#include<opencv2/imgproc/imgproc.hpp>#include<opencv2/highgui/highgui.hpp>using namespace std;using namespace cv;void print_px_value(Mat& im){int rowNumber = im.rows; //行数int colNumber = im.cols * im.channels(); //列数 x 通道数=每一行元素的个数//双重循环,遍历所有的像素值for (int i = 0; i < rowNumber; i++) //行循环{uchar* data = im.ptr<uchar>(i); //获取第i行的首地址for (int j = 0; j < colNumber; j++) //列循环{ //data[j] = data[j] / div * div + div / 2;cout << (int)data[j] << endl;} //行处理结束}}int main(){Mat srcImage = imread("./1.png", 0);print_px_value(srcImage);waitKey(0);}

或者:

int width = im.cols;int height = im.rows;for (int i = 0; i < height; i++){for (int j = 0; j < width; j++){im.at<uchar>(i, j) = 255;}}

//----------------------------------【colorReduce( )函数】-------------------------------//描述:使用【动态地址运算配合at】方法版本的颜色空间缩减函数//----------------------------------------------------------------------------------------------void colorReduce(Mat& inputImage, Mat& outputImage, int div) { //参数准备outputImage = inputImage.clone(); //拷贝实参到临时变量int im_h = outputImage.rows; //行数int im_w = outputImage.cols; //列数//存取彩色图像像素for(int i = 0;i < im_h;i++) { for(int j = 0;j < im_w;j++) // ------------------------【开始处理每个像素】--------------------outputImage.at<Vec3b>(i,j)[0] = outputImage.at<Vec3b>(i,j)[0]/div*div + div/2; //蓝色通道outputImage.at<Vec3b>(i,j)[1] = outputImage.at<Vec3b>(i,j)[1]/div*div + div/2; //绿色通道outputImage.at<Vec3b>(i,j)[2] = outputImage.at<Vec3b>(i,j)[2]/div*div + div/2; //红是通道// -------------------------【处理结束】----------------------------} // 行处理结束} }

Mat矩阵中数据指针Mat.data是uchar类型指针,CV_8U系列可以通过计算指针位置快速地定位矩阵中的任意元素。

Mat M(7,7,CV_32F,Scalar(1,3));

解释如下:创建一个M矩阵,7行7列,类型为CV_32F,C2表示有2个通道。Scalar(1,3)是对矩阵进行初始化赋值。第一个通道全为1,第2个通道全为3。

Mat_对应的是CV_8U,

Mat_对应的是CV_8S,

Mat_对应的是CV_32S,

Mat_对应的是CV_32F,

Mat_对应的是CV_64F

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。