Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenCV:输出图像为蓝色_C++_Image_Opencv_Opencv3.0 - Fatal编程技术网

C++ OpenCV:输出图像为蓝色

C++ OpenCV:输出图像为蓝色,c++,image,opencv,opencv3.0,C++,Image,Opencv,Opencv3.0,所以我在做这个项目,我在OpenCV上做一个图像的反射(不使用flip函数),完成它的唯一问题(我想)是,应该反射出来的图像,是全蓝色的 我的代码(我取出了通常的部分,问题应该在这里): Mat imageReflectionFinal=Mat::零(大小(220220),CV_8UC3); 对于(unsigned int r=0;r,您的代码存在一些问题。如前所述,您的迭代变量超出了实际图像维度。请不要使用硬编码边界,您可以使用inputImage.cols和inputImage.rows来获

所以我在做这个项目,我在OpenCV上做一个图像的反射(不使用flip函数),完成它的唯一问题(我想)是,应该反射出来的图像,是全蓝色的

我的代码(我取出了通常的部分,问题应该在这里):

Mat imageReflectionFinal=Mat::零(大小(220220),CV_8UC3);

对于(unsigned int r=0;r,您的代码存在一些问题。如前所述,您的迭代变量超出了实际图像维度。请不要使用硬编码边界,您可以使用
inputImage.cols
inputImage.rows
来获取图像维度

有一个变量(BGR Vec3b)已设置但未使用-
Vec3b intensity=image.at(r,c);

最重要的是,不清楚您想要实现什么。行
(uchar)(c,-r+(220)/2);
不会给出太多信息。此外,您要将原始图像翻转到哪个方向?X轴还是Y轴

以下是一个可能的解决方案,可将图像沿X方向翻转:

//get input image:
cv::Mat testMat = cv::imread( "lena.png" );

//Get the input image size:
int matCols = testMat.cols;
int matRows = testMat.rows;

//prepare the output image:
cv::Mat imageReflectionFinal = cv::Mat::zeros( testMat.size(), testMat.type() );

//the image will be flipped around the x axis, so the "target"
//row will start at the last row of the input image:
int targetRow = matRows-1;

//loop thru the original image, getting the current pixel value:
for( int r = 0; r < matRows; r++ ){
    for( int c = 0; c < matCols; c++ ) {
        //get the source pixel:
        cv::Vec3b sourcePixel = testMat.at<cv::Vec3b>( r , c );
        //source and target columns are the same:
        int targetCol = c;
        //set the target pixel
        imageReflectionFinal.at<cv::Vec3b>( targetRow , targetCol ) = sourcePixel;
    }
    //for every iterated source row, decrease the number of
    //target rows, as we are flipping the pixels in the x dimension:
    targetRow--;
}
//获取输入图像:
cv::Mat testMat=cv::imread(“lena.png”);
//获取输入图像大小:
int matCols=testMat.cols;
int matRows=testMat.rows;
//准备输出图像:
cv::Mat imageReflectionFinal=cv::Mat::Zero(testMat.size(),testMat.type());
//图像将围绕x轴翻转,因此“目标”
//行将从输入图像的最后一行开始:
int targetRow=matRows-1;
//循环浏览原始图像,获取当前像素值:
对于(int r=0;r
结果:


您的代码存在一些问题。如前所述,您的迭代变量超出了实际图像尺寸。不要使用硬编码边界,您可以使用
inputImage.cols
inputImage.rows
来获取图像尺寸

有一个变量(BGR Vec3b)已设置但未使用-
Vec3b intensity=image.at(r,c);

最重要的是,不清楚您想要实现什么。行
(uchar)(c,-r+(220)/2);
不会给出太多信息。此外,您要将原始图像翻转到哪个方向?X轴还是Y轴

以下是一个可能的解决方案,可将图像沿X方向翻转:

//get input image:
cv::Mat testMat = cv::imread( "lena.png" );

//Get the input image size:
int matCols = testMat.cols;
int matRows = testMat.rows;

//prepare the output image:
cv::Mat imageReflectionFinal = cv::Mat::zeros( testMat.size(), testMat.type() );

//the image will be flipped around the x axis, so the "target"
//row will start at the last row of the input image:
int targetRow = matRows-1;

//loop thru the original image, getting the current pixel value:
for( int r = 0; r < matRows; r++ ){
    for( int c = 0; c < matCols; c++ ) {
        //get the source pixel:
        cv::Vec3b sourcePixel = testMat.at<cv::Vec3b>( r , c );
        //source and target columns are the same:
        int targetCol = c;
        //set the target pixel
        imageReflectionFinal.at<cv::Vec3b>( targetRow , targetCol ) = sourcePixel;
    }
    //for every iterated source row, decrease the number of
    //target rows, as we are flipping the pixels in the x dimension:
    targetRow--;
}
//获取输入图像:
cv::Mat testMat=cv::imread(“lena.png”);
//获取输入图像大小:
int matCols=testMat.cols;
int matRows=testMat.rows;
//准备输出图像:
cv::Mat imageReflectionFinal=cv::Mat::Zero(testMat.size(),testMat.type());
//图像将围绕x轴翻转,因此“目标”
//行将从输入图像的最后一行开始:
int targetRow=matRows-1;
//循环浏览原始图像,获取当前像素值:
对于(int r=0;r
结果:


两个for循环中都有一个off by one错误,这将导致您访问超过像素缓冲区末尾的内存。
Size(220220)
意味着最大列(以及行)数是219,而不是220。您希望
(uchar)(c,-r+(220)/2)
要做什么?两个for循环中都有一个off by-one错误,这将导致您访问超过像素缓冲区末尾的内存。
大小(220220)
意味着最大列(以及行)数是219,而不是220。您希望
(uchar)(c,-r+(220)/2)要清除它,我的问题是让OP思考C++中的语句实际上意味着什么。很好的回答问题。我不知道我可以得到这样的行和列,谢谢你,而且你发布的准备输出图像的方式似乎更好。我在
(uchar)(c,-r+(220)/2)
中试图做的是应用一个公式(y,−x+2x0),要使图像翻转,我的目标是尝试使其水平翻转(−y+2y0,x)。(更多关于新注释,点击字符限制)此时我尝试将您的一些代码改编为我的代码,如果我将其保留为
Vec3b sourcePixel=image.At(r,c);图像反射率最终在(r,c)=(uchar)(c,-r+(220)/2)
我得到了之前得到的蓝色图像,但是如果我将其更改为
imageReflectionFinal.at(rows,cols)=(uchar)(cols,-rows+(220)/2)
我得到一个空白图像(全黑)。我以前得到的蓝色图像,水平线有一条直线。为了澄清它,我对这句话的问题是让OP思考C++中的语句实际上意味着什么。回答得很好。:)首先,非常感谢您的完整答案!我不知道我可以得到这样的行和列,谢谢你,而且你发布的准备输出图像的方式似乎更好。我在
(uchar)(c,-r+(220)/2)
中试图做的是应用一个公式(y,−x+2x0),使图像翻转,我的obje