Doubt MATLAB code for Opencv C++

Asked

Viewed 22 times

0

Good evening, everyone!

I would like some help in relation to passing a code that is in Matlab to opencv c++. I’m trying to do some operations with rgb channels, but Thresh’s value is not the same - I’m getting the same image. Would someone please help me?

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 + double((1.262*G(i,j))-(0.884*R(i,j))-(0.311*B(i,j)));
    end
end

C++

#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;
}
  • img.cols and img.rows are the number of columns and rows? If so then the comparison in the two loops should probably be < instead of <=.

  • int thresh = 0; shouldn’t be double thresh = 0;?

  • I tried to use only < and also the value is not hitting. I need Thresh to return me an integer value to split by pixels later. I deleted the post with the images, thanks for warning!

  • I don’t use opencv to help more, but since the code is very simple you can run both programs in debug mode seeing the value of thresh both at each iteration of the innermost loop.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.