This is a basic example for the OpenCV
First we must know the structure of IplImage:
IPL image:
IplImage
|-- int nChannels; // Number of color channels (1,2,3,4)
|-- int depth; // Pixel depth in bits:
| // IPL_DEPTH_8U, IPL_DEPTH_8S,
| // IPL_DEPTH_16U,IPL_DEPTH_16S,
| // IPL_DEPTH_32S,IPL_DEPTH_32F,
| // IPL_DEPTH_64F
|-- int width; // image width in pixels
|-- int height; // image height in pixels
|-- char imageData; // pointer to aligned image data
| // Note that color images are stored in BGR order
|-- int dataOrder; // 0 - interleaved color channels,
| // 1 - separate color channels
| // cvCreateImage can only create interleaved images
|-- int origin; // 0 - top-left origin,
| // 1 - bottom-left origin (Windows bitmaps style)
|-- int widthStep; // size of aligned image row in bytes
|-- int imageSize; // image data size in bytes = heightwidthStep
|-- struct _IplROI roi;// image ROI when not NULL specifies image
| // region to be processed
|-- char imageDataOrigin; // pointer to the unaligned origin of image data
| // (needed for correct image deallocation)
|
|-- int align; // Alignment of image rows: 4 or 8 byte alignment
| // OpenCV ignores this and uses widthStep instead
|-- char colorModel[4]; // Color model - ignored by OpenCV
//------------------------------------------------------------------------------int main(int argc, char argv[])
{
IplImage img=cvLoadImage("c://fruitfsbmp",1);
CvScalar s;
for(int i=0;i<img->height;i++){
for(int j=0;j<img->width;j++){
s=cvGet2D(img,i,j); // get the (i,j) pixel value
printf("B=%f, G=%f, R=%f ",sval[0],sval[1],sval[2]);
sval[0]=111;
sval[1]=111;
sval[2]=111;
cvSet2D(img,i,j,s);//set the (i,j) pixel value
}
}
cvNamedWindow("Image",1);
cvShowImage("Image",img);
cvWaitKey(0); //等待按键
cvDestroyWindow( "Image" );//销毁窗口
cvReleaseImage( &img ); //释放图像
return 0;
}
其实还有更好的方法,例如将其封装成类,调用起来更加方便,效率也很高。
要想捕获当前线程内(也就是当前CxxxApp)所有窗口上光标所在的像素颜色,需要重载CxxxApp的PreTranslateMessage函数,加:
if ( pMSG->message == WM_MOUSEMOVE)
{
CPoint ptScreen;
GetCursorPos(&ptScreen);
CWndDeskWnd = GetDesktopWindow();
CDCDeskDC = DeskWnd->GetDC();
COLORREF clr = DeskDC->GetPixel(ptScreen);
DeskWnd->ReleaseDC(DeskDC);
CString strShow;
strShowFormat("%d", clr);
}
要想捕获当前屏幕任何地方的像素值,要先SetCapute,然后在使用上面的代码。
imread就可以读入图像
坐标就是x,y的值除以分辨率
图像在matlab里就是一个矩阵
用行列号找就可以
比如图像A左上角坐标为(0,0),其坐标(100,100)的点的像素为A(101,101)
try{
File _file = new File("C:/Documents and Settings/mayuanfei/My Documents/女友照片jpg"); //读入文件
Image src = javaximageioImageIOread(_file); //构造Image对象
int wideth=srcgetWidth(null); //得到源图宽
int height=srcgetHeight(null); //得到源图长
Systemoutprintln(wideth+","+height);
}catch(Exception e){
eprintStackTrace();
}
以上就是关于opencv如何计算图像中物体的像素值全部的内容,包括:opencv如何计算图像中物体的像素值、vc++图像处理如何提取像素值、请教用DCMTK如何获得图像的像素值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)