[英]OpenCV + visualStudio , C++ code error trying to access pixels
I'm Joe and I'm a new member of this site. 我是Joe,而且是该网站的新成员。 Let's go to the point...I start programming in c++ with openCV libraries on visual studio but I have a question about image's pixels.. I wrote a code to calculate the average of the alpha channel (the foruth component of a pixel, in addition to B,G,R intensity's values).. enter image description here I fall into an error and I don't know how to fix it.
让我们开始讨论……我开始使用Visual Studio上的openCV库在c ++中进行编程,但是我对图像的像素有疑问。除了B,G,R强度的值)..在此处输入图像描述我陷入了错误,我不知道如何解决。 Here's my code
这是我的代码
#include "stdafx.h"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\core\core.hpp"
#include "opencv2\opencv.hpp"
using namespace cv;
using namespace std;
int main() {
Mat image;
image = imread("C:/Users/Joe/Desktop/prova/paesaggioLuminoso.jpg", 1);
namedWindow("Picture", WINDOW_AUTOSIZE);
imshow("Picture", image);
waitKey(0);
int averageAlpha = 0;
int partial = 0;
for (int i = 0; i < image.rows; i++) {
for (int j = 0; j < image.cols; j++) {
partial = partial +image.at<Vec4b>(i, j)[3];
if (i == image.rows - 1 && j == image.cols - 1) {
averageAlpha = partial / (image.rows * image.cols);
cout << "Average of alpha channel is " << averageAlpha << endl;
}
}
}
return 0;
}
Please note that if I try to calculate average of B intensity (or Green or red) , my program runs correctly . 请注意,如果我尝试计算B强度的平均值(或绿色或红色),则我的程序可以正确运行。
But with this fourth channel I have this error message: Unhandled exception at 0x00007FFC47603FB8 in Secondo Progetto OpenCV.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000162E2FF3E0. 但是,通过第四个通道,我收到以下错误消息:Secondo Progetto OpenCV.exe中0x00007FFC47603FB8的未处理异常:Microsoft C ++异常:内存位置0x000000162E2FF3E0的cv :: Exception。
Assertion failed ((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p 1 * channels())) in cv::Mat::at, file c:\\users\\joe\\desktop\\joe\\opencv\\opencv\\build\\include\\opencv2\\core\\mat.inl.hpp, line 1095 断言失败((unsigned)[i1 * DataType <_Tp> :: channels)<(unsigned)[size.p 1 * channels()))在cv :: Mat :: at中,文件c:\\ users \\ joe \\ desktop \\ joe \\ opencv \\ opencv \\ build \\ include \\ opencv2 \\ core \\ mat.inl.hpp,第1095行
Thanks for the help ! 谢谢您的帮助 ! Have a good day !
祝你有美好的一天 !
EDIT: here's the error I mentioned in the comment 编辑:这是我在评论中提到的错误
Errors in visual studio with a correct code 带有正确代码的Visual Studio中的错误
You're passing as flag '1' which equals IMREAD_COLOR see this: https://github.com/opencv/opencv/blob/master/modules/imgcodecs/include/opencv2/imgcodecs.hpp 您正在传递等于IMREAD_COLOR的标志'1',请参见: https : //github.com/opencv/opencv/blob/master/modules/imgcodecs/include/opencv2/imgcodecs.hpp
And IMREAD_COLOR drops the alpha channel, see this https://docs.opencv.org/3.1.0/d4/da8/group__imgcodecs.html#ga61d9b0126a3e57d9277ac48327799c80 并且IMREAD_COLOR会删除Alpha通道,请参见此https://docs.opencv.org/3.1.0/d4/da8/group__imgcodecs.html#ga61d9b0126a3e57d9277ac48327799c80
so you should be doing 所以你应该做
image = imread("C:/Users/Joe/Desktop/prova/paesaggioLuminoso.jpg", IMREAD_UNCHANGED);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.