C++ 不同thresh值的Matlab代码到OpenCV代码的转换
我想要一些关于将matlab中的代码传递给opencv c++的帮助。我正在尝试对RGB通道进行一些操作,但是,thresh的值并不相同-我正在发送相同的图像。谁能帮帮我吗 MATLABC++ 不同thresh值的Matlab代码到OpenCV代码的转换,c++,matlab,opencv,C++,Matlab,Opencv,我想要一些关于将matlab中的代码传递给opencv c++的帮助。我正在尝试对RGB通道进行一些操作,但是,thresh的值并不相同-我正在发送相同的图像。谁能帮帮我吗 MATLAB im = imread('1.png'); [m,n,p] = size(im); R=im(:, :, 1); G=im(:, :, 2); B=im(:, :, 3); thresh=0; for j=1:n for i=1:m thresh = thresh + doubl
im = imread('1.png');
[m,n,p] = size(im);
R=im(:, :, 1);
G=im(:, :, 2);
B=im(:, :, 3);
thresh=0;
for j=1:n
for i=1:m
thresh = thresh + double((1.262*G(i,j))-(0.884*R(i,j))-(0.311*B(i,j)));
end
end
C++
#包括
#包括“opencv2/highgui.hpp”
#包括
#包括
使用名称空间std;
使用名称空间cv;
int main(){
Mat img=imread(“1.png”,imread\u颜色);
int thresh=0;
for(int j=0;j第一个错误是for循环的上限值,因为您超出了图像边框的范围
j可能不是您的问题,但您正在阅读img.cols+1
列和img.rows+1
行。感谢您的帮助,但仍然给出了相同的值差异作为一个例子,我不知道matlab是如何计算的,但在方法上没有问题
#include <opencv2/opencv.hpp>
#include "opencv2/highgui.hpp"
#include <opencv2/core/mat.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(){
Mat img = imread("1.png", IMREAD_COLOR);
int thresh = 0;
for(int j = 0; j <= img.cols; j++){
for(int i = 0; i <= img.rows; i++){
Vec3b color = img.at<Vec3b>(i,j);
uchar a = color.val[0], b = color.val[1], c = color.val[2];
thresh += double((1.262*b)-(0.884*c)-(0.311*a));
}
}
cout << thresh;
return 0;
}
thresh += double((1.262*b)-(0.884*c)-(0.311*a));
thresh += double((1.262*static_cast<double>(b))
-(0.884*static_cast<double>(c))
-(0.311*static_cast<double>(a)));
#include <opencv2/opencv.hpp>
#include "opencv2/highgui.hpp"
#include <opencv2/core/mat.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("img.jpg", IMREAD_COLOR);
double thresh = 0.0;
resize(img,img,Size(100,100));
for(int j = 0; j < img.cols; j++){
for(int i = 0; i < img.rows; i++){
// 1ST WAY
Vec3b color = img.at<Vec3b>(i,j);
uchar a = color.val[0], b = color.val[1], c = color.val[2];
thresh += double((1.262*static_cast<double>(b))
-(0.884*static_cast<double>(c))
-(0.311*static_cast<double>(a)));
// 2ND WAY
// thresh += double((1.262 * (double)img.at<Vec3b>(Point(i,j))[1])
// - (0.884*(double)img.at<Vec3b>(Point(i,j))[2])
// - (0.311 * (double)img.at<Vec3b>(Point(i,j))[0]));
}
}
cout << thresh << endl;
return 0;
}