如何将IPL_DEPTH_8U格式的图片转换为IPL_DEPTH_32S格式?
我需要使用分水岭函数,并且我在IPL_DEPTH_8U中有一个标记图像。问题是分水岭函数只接受标记的IPL_DEPTH_32S。
提前谢谢你,
发布于 2013-02-26 21:54:47
使用Mat而不是IplImage,然后:marker_img.convertTo(marker_img,CV_32S);
如果要使用IplImage,则必须先缩放图像,然后再进行转换
发布于 2013-02-26 23:08:00
如果您不想使用新的cv::Mat版本,可以尝试以下操作:
IplImage* converted = cvCreateImage(cvSize(img->width,img->heigth),IPL_DEPTH_32S);
for (int i = 0;i<img->heigth;i++)
for(int j = 0;j<img-width;j++)
((signed long*)(converted->imageData+i*converted->widthStep))[j] = ((unsigned char*)(img->imageData+i*img->widthStep))[j]如果你有一个3通道的图像!您必须使用以下内容:
IplImage* converted = cvCreateImage(cvSize(img->width,img->heigth),IPL_DEPTH_32S,3);
for (int i = 0;i<img->heigth;i++)
for(int j = 0;j<3*(img-width);j++)//since you have 3 channels
((signed long*)(converted->imageData+i*converted->widthStep))[j] = ((unsigned char*)(img->imageData+i*img->widthStep))[j]以及一种更好的编写代码的说教方式:
for (int i = 0;i<img->heigth;i++)
for(int j = 0;j<(img-width);j++)
{
((signed long*)(converted->imageData+i*converted->widthStep))[j*3] = ((unsigned char*)(img->imageData+i*img->widthStep))[j*3]//R or B ??
((signed long*)(converted->imageData+i*converted->widthStep))[j*3+1] = ((unsigned char*)(img->imageData+i*img->widthStep))[j*3+1]//G
((signed long*)(converted->imageData+i*converted->widthStep))[j*3+2] = ((unsigned char*)(img->imageData+i*img->widthStep))[j*3+2]//R or B ??
}发布于 2013-02-26 23:40:02
使用显式强制转换按每个通道的像素手动执行此操作:
Img32s->imageData[y*((int)Img32s->widthStep)+x*3+C]=(int)((uchar*)Img8u->imageData)[y*((int)Img8u->widthStep)+x*3+C]/255.;https://stackoverflow.com/questions/15088165
复制相似问题