Ask Your Question
0

HELP PLEASE? Image window will not open.

asked 2014-07-03 10:08:51 -0500

brycemh gravatar image

BELOW IS A COPY OF MY CODE. IT BUILDS SUCCESSFULLY, BUT WHEN I RUN THE PROGRAM, THE IMAGE WINDOW WILL NOT OPEN image description

edit retag flag offensive close merge delete

Comments

1

try to add cv::waitkey() after imshow.

GilLevi gravatar imageGilLevi ( 2014-07-03 10:30:25 -0500 )edit

3 answers

Sort by ยป oldest newest most voted
1

answered 2014-07-03 12:07:16 -0500

Try this code: Just make sure the image given to imread exists.

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

int main(int argc, char** argv)
{
    cv::Mat img = cv::imread("dm.jpg");
    cv::Point p1, p2;
    for(int r = 4; r < img.rows; r+=5){
        for(int c = 4; c < img.cols; c +=5){
            //vLines
            p1.x = c;
            p1.y = 0;
            p2.x = c;
            p2.y = img.rows;
            cv::line(img, p1, p2, cv::Scalar(255, 255, 255));
            //hLines
            p1.x = 0;
            p1.y = r;
            p2.x = img.cols;
            p2.y = r;
            cv::line(img, p1, p2, cv::Scalar(255, 255, 255));
        }
    }
    std::string win_name = "MyWindow";
    cv::namedWindow(win_name, CV_WINDOW_AUTOSIZE);
    cv::imshow(win_name, img);
    cv::waitKey();
}
edit flag offensive delete link more

Comments

make sure you understand what's happening in this code.

hadoofi gravatar imagehadoofi ( 2014-07-03 12:08:29 -0500 )edit

@hadoofi Thank you alot! I don't quite understand the loops though; if possible, can you explain? I am new to OpenCV and C++.

brycemh gravatar imagebrycemh ( 2014-07-03 12:33:05 -0500 )edit

The first loop iterates over the rows of the image. As you stated, you need to draw a line every 5th row/col. The first row/col starts at index of zero. Every loop increments by 5 (r+=5 is equivalent to r = r + 5).

For more details, see this tutorial: http://www.cprogramming.com/tutorial/lesson3.html and for nested loops: http://www.tutorialspoint.com/cplusplus/cpp_nested_loops.htm

I hope this helps you! I would help more if you ask specific questions.

hadoofi gravatar imagehadoofi ( 2014-07-03 13:21:29 -0500 )edit
0

answered 2014-07-03 11:36:31 -0500

unxnut gravatar image

When you have a problem like this, the first step will be to just display the picture that you just read. The way you have written your for loops, the loop will execute just once, and that too, only the first loop because then, you will return from main. You are also mixing old C legacy style code and the new C++ interface. You can create the display window using just namedWindow function that you have towards the bottom; the cvNamedWindow towards the top is unnecessary. When drawing the line, use Point instead of cvPoint. And after imshow, add a call to waitKey.

edit flag offensive delete link more
0

answered 2014-07-03 10:36:23 -0500

Please, next time post your code in text rather than a picture.

I don't know what are you trying to do with your code, but remove the 'return' from the for loops you have. In addition, the OpenCV function that draws a line has the following signature:

 void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

For more details; Draw a line by using the OpenCV function

To draw a line you need the image to draw the lines, and two points (and some other parameters mentioned above)

If you state what you want to do clearly, I would help you more.

edit flag offensive delete link more

Comments

One more thing, you already declared 'x' before the for loop so no need to declare another one in the for loop.

hadoofi gravatar imagehadoofi ( 2014-07-03 10:38:32 -0500 )edit

Also, do what GilLevi said in the first comment: add cv::waitKey() before the end of the 'main' function.

hadoofi gravatar imagehadoofi ( 2014-07-03 10:52:15 -0500 )edit

@hadoofi I am trying to draw vertical and horizontal lines for every 5th pixel on my image. Any more help you can give me?

brycemh gravatar imagebrycemh ( 2014-07-03 10:58:51 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-07-03 10:08:51 -0500

Seen: 94 times

Last updated: Jul 03 '14