首页 技术 正文
技术 2022年11月6日
0 收藏 394 点赞 698 浏览 2739 个字

说到图像像素,肯定要先认识一下图像中的坐标系长什么样。

 1. 坐标体系中的零点坐标为图片的左上角,X轴为图像矩形的上面那条水平线;Y轴为图像矩形左边的那条垂直线。该坐标体系在诸如结构体Mat,Rect,Point中都是适用的。(OpenCV中有些数据结构的坐标原点是在图片的左下角,可以设置的)。

 2. 在使用image.at<TP>(x1, x2)来访问图像中点的值的时候,x1并不是图片中对应点的x轴坐标,而是图片中对应点的y坐标(也就是编程中的pic.rows那行)。x2同理。

 3. 如果所画图像是多通道的,比如说image图像的通道数时n,则使用Mat::at(x, y)时,其x的范围依旧是0到image的height,而y的取值范围则是0到image的width乘以n,因为这个时候是有n个通道,所以每个像素需要占有n列。但是如果在同样的情况下,使用Mat::at(point)来访问的话,则这时候可以不用考虑通道的个数,因为你要赋值给获取Mat::at(point)的值时,都不是一个数字,而是一个对应的n维向量。

 4. 多通道图像在使用minMaxLoc()函数时不能给出其最大最小值坐标的,因为每个像素点其实有多个坐标,所以是不会给出的。因此在编程时,这2个位置应该给NULL。

5 多通道的图像可以直接赋值,不必每个通道赋值。但是要注意其类型是Vec3b,如果写成uchar,最后的copy图像只会显示源图像的1/3

    char *tempPath="0.jpg";
Mat src=imread(tempPath);
Mat copy=Mat::zeros(src.rows,src.cols,src.type());
for (int nrows=;nrows<src.rows;nrows++)
{
for (int ncols=;ncols<src.cols;ncols++)
{
copy.at<Vec3b>(nrows,ncols)=src.at<Vec3b>(nrows,ncols); }
}
    Mat src=imread("image/color.jpg");
imshow("a",src);
int i,j;
int cPointR,cPointG,cPointB,cPoint;//currentPoint;
for(i=;i<src.rows;i++)
for(j=;j<src.cols;j++)
{
cPointB=src.at<Vec3b>(i,j)[];
cPointG=src.at<Vec3b>(i,j)[];
cPointR=src.at<Vec3b>(i,j)[];
if(cPointB>&cPointR<&cPointG<)
{
src.at<Vec3b>(i,j)[]=; //单通道是uchar,没有[0][1][2]
src.at<Vec3b>(i,j)[]=;
src.at<Vec3b>(i,j)[]=;
} }
imshow("da",src);

注意每个点的像素灰度值cPointR,cPointG,cPointB,cPoint的数据类型是int,在单通道图内由于读取像素灰度值的代码是img.at<uchar>(nrows,ncols),所以特别容易把数据类型记成unsigned char型

============================================================

【转】VC++读取图像RGB值(未试验)

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")using namespace std;
using namespace Gdiplus;int main()
{
GdiplusStartupInput gdiplusstartupinput;
ULONG_PTR gdiplustoken;
GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL); wstring infilename(L"1.jpg");
string outfilename("color.txt"); Bitmap* bmp = new Bitmap(infilename.c_str());
UINT height = bmp->GetHeight();
UINT width = bmp->GetWidth();
cout << "width " << width << ", height " << height << endl; Color color;
ofstream fout(outfilename.c_str()); for (UINT y = ; y < height; y++)
for (UINT x = ; x < width ; x++)
{
bmp->GetPixel(x, y, &color);
fout << x << "," << y << ";"
<< (int)color.GetRed() << ","
<< (int)color.GetGreen() << ","
<< (int)color.GetBlue() << endl;
} fout.close(); delete bmp;
GdiplusShutdown(gdiplustoken);
return ;
}

关于数据的储存:(转)

Mat_<uchar>对应的是CV_8U,Mat_<char>对应的是CV_8S,Mat_<int>对应的是CV_32S,Mat_<float>对应的是CV_32F,Mat_<double>对应的是CV_64F,对应的数据深度如下:

• CV_8U – 8-bit unsigned integers ( 0..255 )

• CV_8S – 8-bit signed integers ( -128..127 )

• CV_16U – 16-bit unsigned integers ( 0..65535 )

• CV_16S – 16-bit signed integers ( -32768..32767 )

• CV_32S – 32-bit signed integers ( -2147483648..2147483647 )

• CV_32F – 32-bit floating-point numbers ( -FLT_MAX..FLT_MAX, INF, NAN )

• CV_64F – 64-bit floating-point numbers ( -DBL_MAX..DBL_MAX, INF, NAN )

这里还需要注意一个问题,很多OpenCV的函数支持的数据深度只有8位和32位的,所以要少使用CV_64F,但是vs的编译器又会把float数据自动变成double型,有些不太爽。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,782