C++ 灰度C++;使用OpenCV(出现一些噪音)
我有一些问题转换为灰度使用openCV在使手动功能。 这是我的密码 main.cppC++ 灰度C++;使用OpenCV(出现一些噪音),c++,image,opencv,grayscale,C++,Image,Opencv,Grayscale,我有一些问题转换为灰度使用openCV在使手动功能。 这是我的密码 main.cpp unsigned int height, width; int main(int argc, char** argv) { IplImage* image_input = cvLoadImage("duck.jpg", CV_LOAD_IMAGE_UNCHANGED); IplImage* image_output = cvCreateImage(cvGetSize(image_input),IPL_
unsigned int height, width;
int main(int argc, char** argv)
{
IplImage* image_input = cvLoadImage("duck.jpg", CV_LOAD_IMAGE_UNCHANGED);
IplImage* image_output = cvCreateImage(cvGetSize(image_input),IPL_DEPTH_8U,1);
unsigned char *h_out = (unsigned char*)image_output->imageData;
unsigned char *h_in = (unsigned char*)image_input->imageData;
width = image_input->width;
height = image_input->height;
h_grayscale(h_in, h_out);
cvShowImage("Original", image_input);
cvShowImage("CPU", image_output);
cvReleaseImage(&image_input);
cvReleaseImage(&image_output);
waitKey(0);
}
这是我的灰度代码
void h_grayscale( unsigned char* h_in, unsigned char* h_out)
{
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
int index = (i*j)*3;
double temp = 0.3*h_in[index]+0.6*h_in[index+1]+0.1*h_in[index+2];
h_out[i*j] = (unsigned char)temp;
}
}
void h_灰度(无符号字符*h_输入,无符号字符*h_输出)
{
对于(int i=0;i您计算的输入和输出指数不正确。
使用OpenCV图像时要记住的第一点是它们是对齐的,即每一行的末尾都填充了一些随机值。因此,在计算彩色和灰度图像中像素的线性索引时,应使用widthStep
而不是width
计算像素索引的通用公式为:
i * widthStep/sizeof(type) + (channels * j)
其中,i
是行号,j
是列号
将上述公式转换为当前情况,指数计算如下:
width = image_input->width;
height = image_input->height;
colorWidthStep = image_input->widthStep;
grayWidthStep = image_output->widthStep;
输入:
int index = i * colorWidthStep + (3 * j);
h_out[i * grayWidthStep + j] = (unsigned char)temp;
输出:
int index = i * colorWidthStep + (3 * j);
h_out[i * grayWidthStep + j] = (unsigned char)temp;
您可以创建另外两个全局变量colorWidthStep
和grayWidthStep
,以及width
和height
。按如下方式初始化变量:
width = image_input->width;
height = image_input->height;
colorWidthStep = image_input->widthStep;
grayWidthStep = image_output->widthStep;
您计算的输入和输出索引不正确。
使用OpenCV图像时要记住的第一点是它们是对齐的,即每一行的末尾都填充了一些随机值。因此,在计算彩色和灰度图像中像素的线性索引时,应使用widthStep
而不是width
计算像素索引的通用公式为:
i * widthStep/sizeof(type) + (channels * j)
其中,i
是行号,j
是列号
将上述公式转换为当前情况,指数计算如下:
width = image_input->width;
height = image_input->height;
colorWidthStep = image_input->widthStep;
grayWidthStep = image_output->widthStep;
输入:
int index = i * colorWidthStep + (3 * j);
h_out[i * grayWidthStep + j] = (unsigned char)temp;
输出:
int index = i * colorWidthStep + (3 * j);
h_out[i * grayWidthStep + j] = (unsigned char)temp;
您可以创建另外两个全局变量colorWidthStep
和grayWidthStep
,以及width
和height
。按如下方式初始化变量:
width = image_input->width;
height = image_input->height;
colorWidthStep = image_input->widthStep;
grayWidthStep = image_output->widthStep;
谢谢,它很好用。但是我想问一下witdhstep的功能?为什么它应该为输入和输出设置两个widthstep?对不起,我还是图像处理的初学者>。widthstep是图像的实际宽度(实际上,我指的是物理存储在RAM中的宽度)以字节为单位,而不是以像素为单位。如果灰度图像的1像素是1字节,则RGB彩色图像的1像素将是3字节。这意味着RGB图像的宽度步长至少是相同宽度灰度图像的3倍。请看一看。宽度步长也称为步长。@bagusbekam…如果它解决了问题,您可以接受答案。谢谢。对不起,我忘了接受答案。呵呵。我在上面的问题上增加了一个新问题。希望你能再次帮助我。thanks@bagusbekam...嗯,我告诉过你宽度步长对于颜色和灰度是不同的,你必须使用两个不同的变量。如果你不相信我,请打印这些值,自己看看!谢谢,这是rks。但我想问一下witdhstep的功能?以及为什么它应该为输入和输出设置两个widthstep?对不起,我还是图像处理的初学者>。widthstep是图像的实际宽度(实际上,我指的是物理存储在RAM中的宽度)以字节为单位,而不是以像素为单位。如果灰度图像的1像素是1字节,则RGB彩色图像的1像素将是3字节。这意味着RGB图像的宽度步长至少是相同宽度灰度图像的3倍。请看一看。宽度步长也称为步长。@bagusbekam…如果它解决了问题,您可以接受答案。谢谢。对不起,我忘了接受答案。呵呵。我在上面的问题上增加了一个新问题。希望你能再次帮助我。thanks@bagusbekam...我告诉过你宽度步长对于颜色和灰度是不同的,你必须使用两个不同的变量。如果你不相信我,请打印这些值,自己看看!