最近愚蠢的我碰到的问题:
一、如下面一样写是错的:全输出0 0 0
Mat src = imread(srcfolder);
Mat bw = imread(donefolder,CV_LOAD_IMAGE_GRAYSCALE);
int rows = src.rows;
int cols = src.cols;
for (int i = 0; i != rows;i++)
{
uchar *bwdata = bw.ptr<uchar>(i);
for (int j = 0; j != cols;j++)
{
Scalar bwvalue = bwdata[j];
if (bwvalue==Scalar(0))
{
continue;
}
else
{
Vec3b values = src.at<uchar>(j, i);
int blue = values.val[0];
int green = values.val[1];
int red = values.val[2];
cout << red << " " << green << " " << blue << endl;
//int index = red + green * 256 + blue * 256 * 256;
//rgbcsv[index] = 255;
//file << red << " " << green << " " << blue<<"\n";
}
}
}
但改成下面就正确了:
IplImage* src = cvLoadImage(srcfolder);
IplImage* bw = cvLoadImage(donefolder, CV_LOAD_IMAGE_GRAYSCALE);
int srcstep = src->widthStep / sizeof(uchar);
uchar *srcdata = (uchar*)src->imageData;
int bwstep = bw->widthStep / sizeof(uchar);
uchar *bwdata = (uchar*)bw->imageData;
for (int i = 0; i != src->height; i++)
{
for (int j = 0; j != src->width; j++)
{
uchar bwtemp = bwdata[i*bwstep + j + 0];
if (bwtemp == 0)
{
continue;
}
else
{
int tempb = srcdata[i*srcstep + j*src->nChannels + 0];
int tempg = srcdata[i*srcstep + j*src->nChannels + 1];
int tempr = srcdata[i*srcstep + j*src->nChannels + 2];
cout << tempr << " " << tempg << " " << tempb << endl;
//file << tempr << " " << tempg << " " << tempb << "\n";
}
}
}
同样的问题:下面这样错误:
Vec3b values = srcrect.at<uchar>(j, i);
int blue = values.val[0];
int green = values.val[1];
int red = values.val[2];
int index = red + green * 256 + blue * 256 * 256;
int gray = red*0.299 + green*0.587 + blue*0.114;
if (rgbcsv[index]==255)
{
if (gray<17)
{
continue;
}
else
dst.at<uchar>(j, i) = 255;
}
改成下面就正确了:
int dststep = dstimg->widthStep / sizeof(uchar);
uchar* dstdata = (uchar*)dstimg->imageData;
int step = testimg->widthStep / sizeof(uchar);
uchar* data = (uchar*)testimg->imageData;
uchar tempb, tempg, tempr;
for (int i = 0; i < testimg->height; i++)
{
for (int j = 0; j < testimg->width; j++)
{
tempb = data[i*step + j*testimg->nChannels + 0];
tempg = data[i*step + j*testimg->nChannels + 1];
tempr = data[i*step + j*testimg->nChannels + 2];
int rgbpixels = tempr + tempg * 256 + tempb * 256 * 256;
uchar rgbelement = rgbarray[rgbpixels];
if (rgbelement == 255){
uchar gray = tempr*0.299 + tempg*0.587 + tempb*0.114;
if (gray<17)
{
continue;
}
dstdata[i*dststep + j*dstimg->nChannels + 0] = 255;
}
}
}
我知道了:
Mat时不能用at!要用.ptr<uchar>这样的方式才行!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
二、将图像像素值保存为csv文件时遇到的问题:
1、uchar类型的像素值保存为csv时无法用excel打开,里面是乱码的。所以我将uchar转成了int才行;
2、将int型的r g b通过file保存为csv时 中间不能只用一个空格,否则r g b只占了excel的一个单元格,改成转义字符\t 竟然也不行,所以我改成了8个空格就可以了;这样就可以后续被CvMLData读出来;
三、关于Alpha通道
总所周知,opencv在imread图像时默认是BGR通道,也就是CV_LOAD_IMAGE_COLOR (>0) 那么就是三通道的BGR;如果选择是CV_LOAD_IMAGE_UNCHANGED (<0) 那么就是BGRA ,也就是四通道!!!但我刚在linux下OpenCV3.2运行了下 CV_LOAD_IMAGE_UNCHANGED (<0) 这种情况的,然后立刻输出这个图像的channels是多少,结果发现仍然是3!!!!!!这里与我以前以为的不一样!甚是奇怪????
无奈,我通过:
void createAlphaMat(Mat &mat,Mat primer_src_img)
{
for (int i = 0; i < mat.rows; ++i) {
for (int j = 0; j < mat.cols; ++j) {
Vec4b& bgra = mat.at<Vec4b>(i, j);
Vec3b bgr=primer_src_img.at<Vec3b>(i,j);
bgra[0] = bgr[0];
bgra[1] = bgr[1];
bgra[2] = bgr[2];
//bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2]));
bgra[3]=0;
}
}
}
其中,Mat primer_src_img是CV_LOAD_IMAGE_COLOR (>0) 也就是常用的三通道的BGR;但Mat srcimg(2048,2448,CV_8UC4); 是我规定的4通道的,然后通过
createAlphaMat(srcimg,primer_src_img);
然后紧跟着打印srcimg的channels,结果发现
现在输出是 4!!!!这样才满足我要的4通道的图像?!