HELP PLEASE? Image window will not open.
BELOW IS A COPY OF MY CODE. IT BUILDS SUCCESSFULLY, BUT WHEN I RUN THE PROGRAM, THE IMAGE WINDOW WILL NOT OPEN
BELOW IS A COPY OF MY CODE. IT BUILDS SUCCESSFULLY, BUT WHEN I RUN THE PROGRAM, THE IMAGE WINDOW WILL NOT OPEN
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();
}
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.
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
.
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.
Asked: 2014-07-03 10:08:51 -0600
Seen: 360 times
Last updated: Jul 03 '14
try to add cv::waitkey() after imshow.