iplimage* 和uchar*互转,mat 和uchar*

#include "opencv2/opencv.hpp"
using namespace cv;
int main()

 //图片路径

string image_name = "H:\\vegData\\1\\yuanshi\\dongua\\IMG_20170407_132802_BURST1.jpg";

Mat img = imread(image_name);

IplImage *pImg;

int frameWidth = img.cols;
int frameHeight = img.rows;

int framenChannels = 3;

pImg = &(IplImage)img;

int framewidthStep = pImg->widthStep;

uchar *buf, *src;

 //Mat 转uchar

buf=img.data;

    

//uchar 转iplimage

//----------------------------------------------------------------------------------------------------------------------------------
IplImage* _pImg = cvCreateImage(cvSize(frameWidth, frameHeight), 8, framenChannels);
for (int i = 0; i < frameHeight; i++)
{
for (int j = 0; j < frameWidth; j++)
{
_pImg->imageData[(i*frameWidth + j) * 3 + 0] = buf[i*framewidthStep + j * 3 + 0];
_pImg->imageData[(i*frameWidth + j) * 3 + 1] = buf[i*framewidthStep + j * 3 + 1];
_pImg->imageData[(i*frameWidth + j) * 3 + 2] = buf[i*framewidthStep + j * 3 + 2];
}
}

//------------------------------------------------------------------------------------------------------------------------------------------


//iplimage转uchar 
uchar* data = new uchar[IplImage->width * IplImage->height];
memcpy_s(data, IplImage->width * IplImage->height, IplImage->imageData, IplImage->width * IplImage->height);

//uchar 转mat
Mat image(frameHeight, frameWidth, CV_8UC3, buf);
imshow("image", image);
waitKey();




}
#include "cv.h" #include "highgui.h" #include "cxcore.h" /*--------------copyright-hanshanbuleng--------------------*/ // 将色调H的取值范围换到0~180之间 int main() { float H,S,V,H1,S1,V1; IplImage *src = cvLoadImage("F:\\vs2010program\\RGB_HSV\\study_test\\2.jpg", 1); IplImage *hsv_img = cvCreateImage(cvGetSize(src), 8 , 3); IplImage *h_img = cvCreateImage(cvGetSize(src), 8, 1); IplImage *s_img = cvCreateImage(cvGetSize(src), 8, 1); IplImage *v_img = cvCreateImage(cvGetSize(src), 8, 1); cvCvtColor(src, hsv_img, CV_BGR2HSV); cvSplit(hsv_img, h_img, s_img, v_img, NULL); for(int y = 0; y < hsv_img->height; y++){ for(int x = 0; x < hsv_img->width; x++) { H1 = cvGetReal2D(h_img, y, x); S1 = cvGetReal2D(s_img, y, x); V1 = cvGetReal2D(v_img, y, x); //地址法 H = (uchar)h_img->imageData[y*h_img->widthStep + x*h_img->nChannels]; S = (uchar)s_img->imageData[y*s_img->widthStep + x*s_img->nChannels]; V = (uchar)v_img->imageData[y*v_img->widthStep + x*v_img->nChannels]; printf("H:%f S:%f V:%f \n",H,S,V); } } cvNamedWindow("hsv_img", 0); //HSV图 cvShowImage("hsv_img", hsv_img); cvNamedWindow("h_img", 0); //H通道 cvShowImage("h_img", h_img); cvNamedWindow("s_img", 0); //S通道 cvShowImage("s_img", s_img); cvNamedWindow("v_img", 0); //V通道 cvShowImage("v_img", v_img); cvWaitKey(0); cvReleaseImage(&hsv_img); cvReleaseImage(&h_img); cvReleaseImage(&s_img); cvReleaseImage(&v_img); cvDestroyWindow("hsv_img"); cvDestroyWindow("h_img"); cvDestroyWindow("s_img"); cvDestroyWindow("v_img"); return 0; }
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值