'Can't draw basic shapes with OpenCV and C++: where is the error?
I'm trying to draw some basic shapes on a blank image using OpenCV and C++. Here's the example code I'm trying to run:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp> // drawing shapes
#include <iostream>
#define CV_FILLED -1
int main( int argc, char** argv )
{
// First create a black image.
cv::Mat image(500,500, CV_8UC3, cv::Scalar(0,0,0));
cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
cv::imshow( "Display window", image );
cv::waitKey(0);
// Check if the image is created successfully.
if( !image.data ){
std::cout << "Could not open or find the image" << std::endl ;
exit(EXIT_FAILURE);
}
//####################( Draw Line )##########################
cv::Point p1(100,100), p2(200,100);
cv::Scalar colorLine(0,255,0); // Green
int thicknessLine = 2;
cv::line(image, p1, p2, colorLine, thicknessLine);
//####################( Draw Circle )#########################
// unfilled circle
cv::Point centerCircle1(250,250);
int radiusCircle = 30;
cv::Scalar colorCircle1(0,0,255);
int thicknessCircle1 = 2;
cv::circle(image, centerCircle1, radiusCircle, colorCircle1, thicknessCircle1);
// filled circle
cv::Point centerCircle2(400,100);
cv::Scalar colorCircle2(0,100,0);
cv::circle(image, centerCircle2, radiusCircle, colorCircle2, CV_FILLED);
//####################( Draw Rectangle )#######################
// unfilled
cv::Point p3(400,400), p4(450,450);
cv::Scalar colorRectangle1(0,0,255);
int thicknessRectangle1 = 3;
cv::rectangle(image, p3, p4, colorRectangle1,thicknessRectangle1);
// filled
cv::Point p5(100,400), p6(150,450);
cv::Scalar colorRectangle2(255,0,255);
cv::rectangle(image, p5, p6, colorRectangle2, CV_FILLED);
//#################( Draw Shapes on Image )######################
cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
cv::imshow( "Display window", image );
cv::waitKey(0);
return 0;
}
The output I'm getting looks like this:

While I expect to get this:

It happens whenever I try using cv::circle or cv::rectangle to draw basic shapes.
For example, I tried running the following:
// C++ program to demonstrate rectangle
// over a self-formed background image
#include <iostream>
#include <opencv2/core/core.hpp>
// Drawing shapes
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
// Driver Code
int main(int argc, char** argv)
{
// Creating a blank image with
// white background
Mat image(500, 500, CV_8UC3,
Scalar(255, 255, 255));
// Check if the image is created
// successfully or not
if (!image.data) {
std::cout << "Could not open or "
<< "find the image\n";
return 0;
}
// Top Left Corner
Point p1(30, 30);
// Bottom Right Corner
Point p2(255, 255);
int thickness = 2;
// Drawing the Rectangle
rectangle(image, p1, p2,
Scalar(255, 0, 0),
thickness, LINE_8);
// Show our image inside a window
imshow("Output", image);
waitKey(0);
return 0;
}
And the result I got was:

Where is the error?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
