How to blit the opencv image to window form? - winforms

I am using open-CV to take a live stream from a webcam.
Is there a way to show an IplImage in a picturebox?
I'm using opencv 2.3.1 in C++. I'm working with Visual Studio 2010 in window form application. Thank you
int main()
{
CvCapture* capture=cvCreateCameraCapture(0);
cvNamedWindow("Live",CV_WINDOW_AUTOSIZE);
IplImage *frame=cvCreateImage(cvSize(w,h),8,3); //Original Image
while(1)
{
IplImage *fram=cvQueryFrame(capture);
if(!fram)
break;
cvShowImage("Live",frame);
char c=cvWaitKey(33);
if(c==27)
break;
}
cvReleaseCapture(&capture);
cvDestroyAllWindows();
}
I know this was discussed at
how to put an Iplimage on a picturebox?
but I am not understand how to write in window form.

Related

AVFrame and SDL2 for screen capture

In that last question, I realized I cannot use both SDL and SDL2. Definitely I choose SDL2 over its precedent.
I had written the following code to capture screen and convert it to an AVFrame for video creation:
AVFrame *frame = av_frame_alloc();
...
SDL_Surface *screen = SDL_SetVideoMode(width,height, 0, 0);
SDL_Overlay *bmp = SDL_CreateYUVOverlay(width,height, SDL_YV12_OVERLAY, screen);
if(av_frame_make_writable(frame) < 0)
exit(1);
SDL_LockYUVOverlay(bmp);
memcpy(frame->data[0], bmp->pixels[0], bmp->pitches[0]);
memcpy(frame->data[1], bmp->pixels[1], bmp->pitches[1]);
memcpy(frame->data[2], bmp->pixels[2], bmp->pitches[2]);
SDL_UnlockYUVOverlay(bmp);
The problem is that SDL2 does not support SDL_Overlay. The alternative methods are good for playing a video not capturing it. How should I modify this code to replace SDL_Overlay and make it suitable for SDL2?

OpenCV canny; output image is pure gray

I am learning opencv and reading a book and following examples. The book introduced the canny filter. However there is some problem with my output. As an input image I have given a 512x512 gray scale image but the filter output is pure gray image. Here is the image:
This is the input image.
And this is the output image.
And here is the snippets:
#include <opencv\cv.h>
#include <opencv2\highgui\highgui.hpp>
#include "Resources.h"
IplImage* doCanny(
IplImage* in,
double lowThresh,
double highThresh,
double aperture
) {
if (in->nChannels != 1)
{
return 0; // Canny only handle gray scale images.
}
IplImage* out = cvCreateImage(
CvSize(cvGetSize(in)),
IPL_DEPTH_8U,
1
);
cvCanny(in, out, lowThresh, highThresh, aperture);
return out;
}
int main(int argc, char** argv)
{
IplImage* image = cvLoadImage(IMAGE_FRUIT);
IplImage* output = doCanny(image, 200, 201, 1);
cvNamedWindow("Canny", CV_WINDOW_AUTOSIZE);
cvShowImage("Canny", output);
cvWaitKey(0);
cvReleaseImage(&output);
cvDestroyWindow("Canny");
return 0;
}
Visual Studio 2015, OpenCV version 2.4.13
I think if you step through your code, you will realize the cvCanny function never gets triggered, the returned output from doCanny is a null pointer.
OpenCV's Canny edge detection algorithm only accepts gray scale image, which is why the original code has the "if (in->nChannels != 1)" check, so you need to convert your input image into a grayscale image first.
// Convert to grayscale first
IplImage* gray_image = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
cvCvtColor(image, gray_image, CV_BGR2GRAY);
// Perform Canny
IplImage* output = doCanny(gray_image, 200, 201, 3);
Additional, I think your "aperture" parameter for cvCanny is also invalid, try to use the default value 3 (or 5, 7), and you should be able to see the result.
I would also recommend using the C++ interface instead of the deprecated C interface.

Can I run Gif with c program?

I know that I can display a .png file, So I was thought that i can display a .gif file.
I am using opencv and i will send the code for the displaying of the .png file (that is working when you change the directory to a valid one).
#include <stdio.h>
#include <opencv2\highgui\highgui_c.h>
int main(void)
{
int i;
cvNamedWindow("Display window", CV_WINDOW_AUTOSIZE); //create a window
//create an image
IplImage* image = cvLoadImage("C:\\c1.png", 1); //change to a valid directory
if (!image)//The image is empty.
{
printf("could not open image\n");
}
else
{
cvShowImage("Display window", image);
cvWaitKey(0);
system("pause");
cvReleaseImage(&image);
}
return 0;
}
unfortunately there is no direct gif support in opencv.
cv2.imread(image.gif) is not possible.But there are many other alternatives to insert a GIF
You can use moviepy module to make it happen
Link for moviepy
Here is an example:
Converting a video excerpt into a GIF
importing moviepy module
from moviepy.editor import *
Then a video file is opened and selected the part between 1’22.65 (1
minute 22.65 seconds) and 1’23.2, reduce its size (to 30% of the
original) and save it as a GIF
clip = (VideoFileClip("frozen_trailer.mp4").subclip((1,22.65),(1,23.2)).resize(0.3))
clip.write_gif("use_your_head.gif")

OpenCV show both incoming video and modified video in separate windows

This should be easy. I have a video stream coming in from my webcam. I'm just playing with image transformation etc. I'd like to be able to view the original images (video input) in one window and the transformed video in another. Problem is, as soon as I start capturing video instead of just single images, the original video window displays transformed video. I don't understand why.
cvNamedWindow("in", CV_WINDOW_AUTOSIZE);
cvNamedWindow("out", CV_WINDOW_AUTOSIZE);
CvCapture *fc = cvCaptureFromCAM(0);
IplImage* frame = cvQueryFrame(fc);
if (!frame) {
return 0;
}
IplImage* greyscale = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
IplImage* output = cvCreateImage(cvGetSize(frame),IPL_DEPTH_32F , 1);
while(1){
frame= cvQueryFrame(fc);
cvShowImage("in", frame);
// manually convert to greyscale
for (int y = 0; y < frame->height; y++) {
uchar* p = (uchar*) frame->imageData + y* frame->widthStep; // pointer to row
uchar* gp = (uchar*) greyscale->imageData + y*greyscale->widthStep;
for(int x = 0; x < frame->width; x++){
gp[x] = (p[3*x] + p[3*x+1] + p[3*x+2])/3; // average RGB values
}
}
cvShowImage("out", greyscale);
char c = cvWaitKey(33);
if (c == 27) {
return 0;
}
}
In this simple example, both video streams end up appearing greyscale... The pointer values and imagedata for frame and greyscale are totally different. If I stop showing greyscale in the "out" window, then frame will appear in color.
Also, if I continue and apply a Sobel operation on the greyscale image and display the result in "out", both "in" and "out" windows will show the Sobel image!
Any ideas?
Hmm This was weird, but it seems using CV_WINDOW_AUTOSIZE was the problem? Perhaps it's not supported in OpenCV 2.1 (which I'm pretty sure is what I'm running). Anyways, using 0 instead of CV_WINDOW_AUTOSIZE when creating the windows works fine.
I have tried your code with openCV 2.0 under mandriva 2010 and it is working fine either with CV_WINDOW_AUTOSIZE or 0.
You may try to convert to grayscale with cvCvtColor(frame,grayscale,CV_RGB2GRAY) and see if the problem persist.

Are 2 simultaneous webcam windows possible with openCV?

I am applying common image transforms to my live webcam capture. I want to display the original webcam in one window and the image with the transforms applied to in another window. However, I am getting same image (filtered) on both windows, I am wondering if I am limited by the OpenCV API or if I am missing something? My code snippet looks like -
/* allocate resources */
cvNamedWindow("Original", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Filtered", CV_WINDOW_AUTOSIZE);
CvCapture* capture = cvCaptureFromCAM(0);
do {
IplImage* img = cvQueryFrame(capture);
cvShowImage("Original", img);
Filters* filters = new Filters(img);
IplImage* dst = filters->doSobel();
cvShowImage("Filtered", dst);
cvWaitKey(10);
} while (1);
/* deallocate resources */
cvDestroyWindow("Original");
cvDestroyWindow("Filtered");
cvReleaseCapture(&capture);
Its possible! Try copying img to another IplImage before sending it to processing and see if that works first.
Yes, I know what you're going to say. But just try that first and see if it does what you want. The code below is just to illustrate what you should do, I don't know if it will work:
/* allocate resources */
cvNamedWindow("Original", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Filtered", CV_WINDOW_AUTOSIZE);
CvCapture* capture = cvCaptureFromCAM(0);
do {
IplImage* img = cvQueryFrame(capture);
cvShowImage("Original", img);
IplImage* img_cpy = cvCreateImage(cvGetSize(img), 8, 3);
img_cpy = cvCloneImage(img);
Filters* filters = new Filters(img_cpy);
IplImage* dst = filters->doSobel();
cvShowImage("Filtered", dst);
/* Be aware that if you release img_cpy here it might not display
* the data on the window. On the other hand, not doing it now will
* cause a memory leak.
*/
//cvReleaseImage( &img_cpy );
cvWaitKey(10);
} while (1);
/* deallocate resources */
cvDestroyWindow("Original");
cvDestroyWindow("Filtered");
cvReleaseCapture(&capture);

Resources