OpenCV: Drawing on an image - image

I am working on a program using the OpenCV library (though I am quite a noob on it). One of the things I need to do is to draw on the image. I looked at the OpenCV drawing functions and they all seem pretty simple (Circle, Line, etc), however the program won't compile! It says this to be exact: error C3861: 'Line': identifier not found.
Is there something I haven't installed? I used the tutorial on http://opencv.willowgarage.com/wiki/VisualC%2B%2B_VS2008 to install OpenCV on Visual Studio 2008 and so far this is the only real problem I have.
Please help me! I need this program working as soon as possible!

The function to draw a line in the OpenCV C API is named cvLine, not Line.

I think you have fallen victim of the following common mistake:
C includes are in #include <opencv/core.h> etc, whereas
C++ includes are:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <oppencv2/highgui/highgui.hpp>
Include these for drawing and showing the image. Use using namespace cv; then
you don't have to write cv::line just line and everything will be working fine.
I had to battle with the very same problem when I began. ;)
(And btw use cv::Mat for c++.)

You can now easily paint on OpenCV images. For this you need to call the setMouseCallback(‘window_name’,image_name) function on opencv. After that you can easily handle the Mouse Callback Function upon your images. Then you need to detect the cv2.EVENT_LBUTTONDOWN, cv2.EVENT_MOUSEMOVE and cv2.EVENT_LBUTTONUP events. By checking the proper boolean condition you need to decide how you like to interact with the OpenCV images.
def paint_draw(event,former_x,former_y,flags,param):
global current_former_x,current_former_y,drawing, mode
if event==cv2.EVENT_LBUTTONDOWN:
drawing=True
current_former_x,current_former_y=former_x,former_y
elif event==cv2.EVENT_MOUSEMOVE:
if drawing==True:
if mode==True:
cv2.line(image,(current_former_x,current_former_y),(former_x,former_y),(0,0,255),5)
current_former_x = former_x
current_former_y = former_y
elif event==cv2.EVENT_LBUTTONUP:
drawing=False
if mode==True:
cv2.line(image,(current_former_x,current_former_y),(former_x,former_y),(0,0,255),5)
current_former_x = former_x
current_former_y = former_y
return former_x,former_y
For details you can see link: How to Paint on OpenCV Images and Save the Image
Output:

Related

OpenCV C++ resize() not found

I installed opencv 2.4.9 for macOS and integrated it with Xcode. However, although it finds most functions, when calling the resize() function, I get the build error 'Use of undeclared identifier resize'.
Can anybody please tell me how to fix this?
You don't mention how you are calling it, but there are two resize functions: a member of Mat that changes the number of rows, and cv::resize() that interpolates to resize an image. For the latter you need imgproc.hpp.
#include <opencv2/imgproc/imgproc.hpp>
//...
cv::resize(src, dst, dst.size(), 0, 0, interpolation);

GUI to view values in image using OpenCV in ubuntu12.04

Is it possible to simultaneously display an image and the pixel,coordinate values based on the mouse pointer positions?
I am asking an OpenCV equivalent of imview function in MATLAB.
You don't need Qt to do that. Just use default OpenCV function imshow to show image and SetMouseCallback to set callback on mouse click.
It can be done using mouse call back events. You can find a good example in \opencv\samples\cpp\grabcut.cpp
I had a few problems trying to do this with OpenCV alone using an old code I wrote a while back. At this point I'm not sure if I missed something or if it's a bug in OpenCV. I'll investigate this further.
But I shared a short, self contained, correct (compilable), example at my repository, check cvImage. It's written in C++ with Qt and OpenCV. It's a Qt application that loads an image with OpenCV and displays the RGB values as the title of the Qt window.
Move the mouse around and place the cursor on top of the pixel that you are interested at to see it's RGB value.
Opencv with Qt support will do that.

How to multiply 2 images in JavaCV

I currently have a binary black and white image that I have used cvThreshold on, and I would like to get the color back on the white part of the image.
From my understanding multiplying the original image with the binary image will result in this effect. I am however unsure how to do that. I am using JavaCV. Ive attempted to:
IplImage img.mul(im2);
And that hasn't really worked. How do I use the mul openCV function with JavaCV? Also if anyone has tips on generally converting opencv code to JavaCV I would be very grateful, the little there is on the JavaCV project page is barely enough to keep me afloat.
I have my own (maybe quite strange :)..) way to find functions working in JavaCV, but in many cases it works. There's OpenCV's wrapper for C# named emguCV , which have very similar functions to this in JavaCV. So if I want e.g find multiplying or adding function I write in google: cvMul emgu or something similar,and here is result of my searching:
Wiki emgu link 1
Wiki emgu link 2
So If you want to multiply 2 IplImages you could do something like that:
IplImage Red=IplImage.create(zdj1.cvSize(),8,1);
IplImage Green=IplImage.create(zdj1.cvSize(),8,1);
IplImage Result=IplImage.create(zdj1.cvSize(),8,1);
cvMul(Red,Green,Result,1);

OpenCV cv::Mat displays empty gray images? Cant find the reason

I just want to display this "img1.jpg" image in a c++ project with using opencv libs for future processes, but it only displays an empty gray window. What is the reason of this. Is there a mistake in this code? please help!
Here is the code;
Mat img1;
char imagePath[256] = "img1.jpg";
img1 = imread(imagePath, CV_LOAD_IMAGE_GRAYSCALE);
namedWindow("result", 1);
imshow("result", img1);
Thanks...
I had the same problem and solved putting waitKey(1); after imshow(). The OpenCV documentation explains why:
This function is the only method in HighGUI that can fetch and handle
events, so it needs to be called periodically for normal event
processing, unless HighGUI is used within some environment that takes
care of event processing.
Thanks #b_froz.
For more detials about this issue,you can refer to: http://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html#imshow
Note This function should be followed by waitKey function which displays the image for specified milliseconds. Otherwise, it won’t display the image. For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame for 25 ms, after which display will be automatically closed. (If you put it in a loop to read videos, it will display the video frame-by-frame)
So,not only waitkey(1)could be put after imshow(),but also waitkey(0) or waitkey(other integers).Here is the explanation of the function waitkey() : http://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html#waitkey
Are you importing the correct library ?
This is other very easy way to load one image:
#define CV_NO_BACKWARD_COMPATIBILITY
#include <cv.h>
#include <highgui.h>
#include <math.h>
main(){
IplImage* img = cvLoadImage("yourPicture.jpg");
cvNamedWindow("Original", 1);
cvShowImage("Original", img);
}
I think you have openCV correctly installed, so yo can type this (Ubuntu):
g++ NameOfYourProgram.cpp -o Sample -I/usr/local/include/opencv/ -L/usr/local/lib -lcv -lhighgui ./sample
The problem you are having is due to the type of your Mat img1. When you load your image with the flag CV_LOAD_IMAGE_GRAYSCALE, the type of your Mat is 0 (CV_8UC1), and the function imshow() is not able to show the image correctly.
You can solve this, converting your Mat to type 16 (CV_8UC3):
img1.convertTo(img1,CV_8UC3);
and then show it with imshow():
imshow("result", img1);
I hope this help.

Is there any way to draw a PNG image on window without using MFC?

I am developing a Windows API application without using MFC.
I am using standard Windows libraries.
How do I draw a PNG image in a window?
Help me with some sample code.
I have tried some codes which are available on the Internet, but all are using MFC.
Take a look at this StackOverflow question. It offers several options which should meet your needs.
Adapted from MSDN:
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;
void draw()
{
// start up GDI+ -- only need to do this once per process at startup
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Rect rect(20,20,50,50);
Graphics grpx(dc);
Image* image = new Image(L"SomePhoto.png");
grpx.DrawImage(Img,rect);
delete image;
// shut down - only once per process
GdiplusShutdown(gdiplusToken);
return;
}
Your choices are: GDI+, WIC(Windows Imaging Component) or libpng
You can use GDI+. See Loading and Displaying Bitmaps.
The below code worked for me. It's free of MFC and can be used straight away to draw PNG images in a window.
Gdiplus::Image image(L"C:\\Logo.png") ;
Gdiplus::Graphics* graphics = Gdiplus::Graphics::FromHDC(GetDC(hWnd));
RectF ImgRect(0,0,y3/10,y3/10) ;
Gdiplus::Status result = graphics->DrawImage(&image, ImgRect);
Thanks for all your support and quick response to solve my problem.
If you know PNG'coding,you can decoding it. So you can draw PNG in any way~

Resources