Ask Your Question
0

unexpected error during execution

asked 2018-08-18 18:31:55 -0500

neto gravatar image

Is a simple program for add +30 in intesitys, but it stop during the 'for'

#include "stdafx.h"


#include < opencv2/opencv.hpp >

#include < iostream >

#include < opencv2/imgproc/imgproc.hpp >


using namespace cv;

using namespace std;

Vec3b logPoint(Vec3b RGB) {
    Vec3b res;
    res[0] = RGB[0]+30;
    res[1] = RGB[1]+30;


    return res;
}

int main(int argc, char** argv)
{


    //ABRE A IMAGEM->CINZA e MOSTRA
    Mat img = imread("img.jpg", IMREAD_GRAYSCALE);
    Mat dest(img.size().width, img.size().height,CV_8SC1);



for (int i = 0; i-1 < img.rows; i++) {
    for (int j = 0; j-1 < img.cols; j++) {
        Vec3b pixel = img.at<Vec3b>(i,j);
        dest.at<Vec3b>(i, j) = logPoint(pixel);

        //cout << logPoint(pixel)[0] << "|" << logPoint(pixel)[1]<<endl;

    }
}

namedWindow("img", WINDOW_AUTOSIZE);
imshow("img", img);
if(waitKey(0) == 27) destroyAllWindows();


return 0;

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-08-18 22:59:49 -0500

berak gravatar image

updated 2018-08-18 23:19:47 -0500

please try to AVOID writing per-pixel loops like that. it is slow and error-prone.

for (int i = 0; i-1 < img.rows; i++) // one-off !!

also, your image is grayscale, but you treat it as a 3 channel one.

the whole double for loop could be a simple:

Mat img = imread("img.jpg", IMREAD_COLOR);
img += Scalar(30, 30, 0); // that's it.

or, for a single channel, even:

Mat img = imread("img.jpg", IMREAD_GRAYSCALE);
img += 30;
edit flag offensive delete link more

Comments

Yep, I noticed this after, this code is only for theories practices . Thanks for your help!

neto gravatar imageneto ( 2018-08-20 11:37:18 -0500 )edit

but why is 'for' not recommended? and I should do what to optimize an operation p to p that is not a simple sum, for example calculate the log of the pixels

neto gravatar imageneto ( 2018-08-20 11:42:04 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-08-18 18:31:55 -0500

Seen: 12 times

Last updated: Aug 18 '18