Can I run Gif with c program? - c

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")

Related

WIN32 : PictureBox Does Not Display Picture

I am using WIN32 and C programming to do a window dialog in Visual Studio 2008 in the Windows CE OS using Windows 5.0 Mobile SDK. I insert my two picture boxes using the Resource Dialog Editor and ensure they are allocated IDs in the window resource. I am using SHLoadDIBitmap to display the image. My code compiles without errors but when I run the program, no images appear in the two picture boxes. What am I doing wrong?
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <windowsx.h>
#include <winuser.h>
#include "ScanCAPI.h"
#include "resource.h"
#pragma comment(lib, "Kernel32.lib")
HBITMAP hImage;
HBITMAP hImage1;
.
.
.
switch(uMsg)
{
case WM_INITDIALOG:
hImage = SHLoadDIBitmap(TEXT("\\My Documents\\image1.bmp"));
hImage1 = SHLoadDIBitmap(TEXT("\\My Documents\\image2.bmp"));
if (hImage==NULL) {
MessageBox(0,"hImage returned null",0,0);
} else {
hnd_pic1 = GetDlgItem(hwnd,IDC_STATIC8);
SendMessage(hnd_pic1, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImage);
}
hnd_pic2 = GetDlgItem(hwnd,IDC_STATIC9);
if (hImage1==NULL) {
MessageBox(0,"hImage1 returned null",0,0);
} else {
SendMessage(hnd_pic2, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImage1);
}
The following code sample works for me on Windows 10 (SHLoadDIBitmap API seems not valid for Windows 10. I use LoadImage API instead.). You can refer to.
C++ code in dialog box procedure:
case WM_INITDIALOG:
hImage = LoadImage(NULL, L"full_path_to\\image3.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
if (NULL == hImage)
errCode = GetLastError();
hwd_static_img = GetDlgItem(hDlg, IDC_STATIC6);
SendMessage(hwd_static_img, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImage);
return (INT_PTR)TRUE;
Resource script in project_name.rc file:
CONTROL "",IDC_STATIC6,"Static", SS_BITMAP,37,133,136,109
Two notes:
Make sure the image file is a valid bitmap file. For example, you can draw a picture and save as a bitmap using mspaint.exe. If you rename a file from .PNG to .BMP, LoadImage will return a NULL handle but GetLastError return 0 which indicate no error.
Make sure set SS_BITMAP static control style for picture control (IDC_STATIC6).
BTW, no need to put image file in same directory with your project or EXE. Specify the valid full path of the image file will work.

How to work with SDL_surface pixel data?

I need to read bmp image file, increase the brightness, and then save the bmp. I decided to use SDL2, because it seemed to the easiest for me to load bmp-s. I wrote a code, but I do not understand how to work with SDL_surface pixels. It is just a pointer to the actual pixel data. How can I increase values, and then save the modified bmp? Here is my try:
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* picture = NULL;
Uint8 red, green, blue;
picture = SDL_LoadBMP("avxsample.bmp");
if (picture != NULL)
{
printf("BMP File is loaded!\n");
// This part is unclear for me...
SDL_SaveBMP(picture, "avxsamlpe_new.bmp");
}
else
{
printf("Cannot load BMP File!\n");
SDL_FreeSurface(picture);
}
return 0;
Anyone can help me?

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.

How to blit the opencv image to window form?

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.

Very Slow Processing of my Opencv Application

I am building an OpenCV application which captures a video from camera and overlays it on another video after removing the background.
I am not able to achieve a reasonable speed as it is playing the output at about 1 fps, whereas my background removal is working at 3fps.
Is there a way to display the background video at its normal speed and overlay the processed video at 3fps ?
I tried commenting out my code and I realized that the problem lies majorly with the Rendering part itself. I tried displaying the video along with my web cam feed and I noticed that there is a drop in the actual fps and the fps of video when displayed with openCV.
here is the sample code:
void main()
{
CvCapture* capture, *Vcap;
capture = cvCaptureFromCAM(0);
if(!capture)
{
printf("Video Load Error");
}
Vcap = cvCaptureFromAVI("bgDemo.mp4");
//printf("\nEntered BGR");
if(!Vcap)
{
printf("Video Load Error");
}
while(1)
{
IplImage* src = cvQueryFrame(Vcap);
if(!src)
{
Vcap = cvCaptureFromAVI("bgDemo.mp4");
continue;
}
IplImage* bck1 = cvCreateImage(cvGetSize(src),8,3);
cvResize(src,bck1,CV_INTER_LINEAR);
cvShowImage("BCK",bck1);
cvWaitKey(1);
}
}
The main problem is that you are allocating a new image at every iteration of the loop without releasing it at the end of the loop. In other words, you have a beautiful memory leak.
A better approach is to simply grab a frame of the video before the loop starts. This will let you create bck1 with the right size just once.
There are other problems with your code, I'm sharing a fixed version below, make sure you pay attention to every line of code to see what changed. I haven't had time to test it, but I'm sure you'll figure it out:
int main()
{
// I know what you are doing, just one capture interface is enough
CvCapture* capture = NULL;
capture = cvCaptureFromCAM(0);
if(!capture)
{
printf("Ooops! Camera Error");
}
capture = cvCaptureFromAVI("bgDemo.mp4");
if(!capture)
{
printf("Ooops! Video Error");
// if it failed here, it means both methods for loading a video stream failed.
// It makes no sense to let the application continue, so we return.
return -1;
}
// Retrieve a single frame from the camera
IplImage* src = cvQueryFrame(capture);
if(!src)
{
printf("Ooops! #1 cvQueryFrame Error");
return -1;
}
// Now we can create our backup image with the right dimensions.
IplImage* bck1 = cvCreateImage(cvGetSize(src),src->depth, src->nChannels);
if(!bck1)
{
printf("Ooops! cvCreateImage Error");
return -1;
}
while(1)
{
src = cvQueryFrame(capture);
if(!src)
{
printf("Ooops! #2 cvQueryFrame Error");
break;
}
cvResize(src, bck1, CV_INTER_LINEAR);
cvShowImage("BCK",bck1);
cvWaitKey(10);
}
cvReleaseImage( &bck1 ); // free manually allocated resource
return 0;
}
These fixes should speed up your application considerably.

Resources