I want to display output image in different windows in OpenCV - c

I want to display the same image multiple times in different windows, for which I have used a for loop but I am getting only one window display. Can anybody provide me any suggestion on how to display output images in multiple windows? Following is the code in OpenCV with C API. Here, I am simply loading an image from argv[1] and trying to display it in 4 different windows.
#include "cv.h"
#include "highgui.h"
#include <stdlib.h>
#include <stdio.h>
int main( int argc, char** argv ) {
int i;
IplImage* img = cvLoadImage( argv[1],1);
cvMoveWindow("Example1", 100, 100);
cvNamedWindow( "Example1", 1);
for(i =0; i<=4;i++) // for loop to display the same image in 4 different windows
{
cvShowImage( "Example1", img );
}
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
}
P.S. I have asked a similar question show multiple images in different window in OpenCV before which was not solved and the code was difficult to understand so I am trying this question with a simpler code.

Here you go
int i;
IplImage* img = cvLoadImage("/home/khashayar/Downloads/bug14.png", 1);
cvMoveWindow("Example1", 100, 100);
cvNamedWindow("Example1", 1);
for (i = 0; i <= 4; i++)
{
char str[5] = "test";
str[4] = i+48;
cvShowImage(str, img);
}
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Example1");

for(i =0; i<=4;i++) // hmm, i<=4 will actually run 5 times ...
{
cvShowImage( "Example1", img ); // <-- same window name for all == only 1 shown
}
but, please discard the c api and use c++ !. please !
#include "opencv2/opencv.hpp"
int main( int argc, char** argv ) {
cv::Mat img = cv::imread( argv[1],1);
for( int i=0; i<4; i++ ) // for loop to display the same image in 4 different windows
{
cv::String name = cv::format("Example%d",i);
cv::namedWindow( name, 1);
cv::imshow( name, img );
}
cv::waitKey(0);
}

Related

Can't create ppm File from an executable (C)

I am trying to create a ppm file from inside a C program, but somehow it doesn't work.
It works fine when I am inside the IDE and run the program there. The program is executed and the file created inside the file's folder.
But once I build it and open the executable with a double click, the program runs in the terminal but does not create the file.
I am working on a mac and this is the relevant code, thanks in advance you all!
// from generalSettings.h
#define WIDTH 1100
#define HEIGHT 966
#define MYFILENAME "testimage.ppm"
int pictureArray[HEIGHT][WIDTH];
//from the main.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "generalSettings.h"
int main()
{
triangle firstParentTriangle;
// these two functions "draw" sierpinsky triangles in the pictureArray
drawFirstParentTriangle(&firstParentTriangle);
drawChildTriangles(firstParentTriangle, numberOfRecursions);
create_ppm();
return 0;
}
void create_ppm()
{
unsigned char color_black[] = "000 000 000\n";
unsigned char color_white[] = "255 255 255\n";
FILE *p_file = fopen(MYFILENAME, "w");
if (NULL != p_file)
{
fprintf(p_file, "P3\n %d %d\n 255\n", WIDTH, HEIGHT);
for (int i = 0; i < HEIGHT; i++)
for (int j = 0; j < WIDTH; j++)
if (1 == pictureArray[i][j])
fprintf(p_file, color_black);
else
fprintf(p_file, color_white);
fclose(p_file);
}
}
Here, I have replaced the foreground and background variables with local ones.
Generally, the program inserts 1 to the pictureArray[][] on certain elements and leaves others with a 0.
For my problem, this should be the relevant code for a reproducable example.
EDIT: Problem solver. File was created in the user folder due to missing path.

How to write numbers in text files in embedded C

I'm working with an EFM32wg280f256 and I would like to debug the code that I'm writing in the following manner: opening a file in SD memory and write the content of the buffers I'm using.
This is a minimal example of my attempt:
#include <stdbool.h>
#include "ff.h"
#include <stdio.h>
#include <complex.h>
#include "arm_math.h"
#include "audioMoth.h"
#define NUMBER_OF_SAMPLES_IN_BUFFERS_DATA 4
static float32_t* buffersDATA[12];
int main(void) {
//Create buffers
buffersDATA[0] = (float32_t*)AM_EXTERNAL_SRAM_START_ADDRESS;
for (int i = 1; i < 12; i += 1) {
buffersDATA[i] = buffersDATA[i - 1] + NUMBER_OF_SAMPLES_IN_BUFFERS_DATA;
}
//Example of collected data
float32_t var0[] = {-29.499557,-67.498978,-54.499176,-53.499191};
//Pass collected data to one of created buffers
for (int j = 0; j <NUMBER_OF_SAMPLES_IN_BUFFERS_DATA; j+= 1){
*(buffersDATA[0]+j) = var0[j];
}
//Initialize file system
AudioMoth_enableFileSystem();
// Write text file
FIL fpt;
f_open(&fpt,"dataVAR.txt", FA_CREATE_ALWAYS | FA_WRITE);
for (int i = 0; i <NUMBER_OF_SAMPLES_IN_BUFFERS_DATA; i+= 1){
char str[8];
sprintf(str, "%d, ", (int)var0[i]);
f_puts(str,&fpt);
}
f_close(&fpt);
// Write another text file
FIL fptr;
f_open(&fptr,"data.txt", FA_CREATE_ALWAYS | FA_WRITE);
for (int i = 0; i <NUMBER_OF_SAMPLES_IN_BUFFERS_DATA; i+= 1){
char str[8];
sprintf(str, "%d, ", (int)*(buffersDATA[0]+i));
f_puts(str,&fptr);
}
f_close(&fptr);
}
Typecasting is because sprintf does not support float values, but integer is enough for me to know if I am doing OK or not.
When I open dataVAR.txt:
-29, -67, -54, -53,
But data.txt:
0, 0, 0, 0,
when they should be the same.
I've tried the same in a executable (adapting it) to verify that I am correctly passing the values (it seems so).
Where is the problem?
Thanks in advance.
Ok, the problem was that I didn't initialize communication between the microcontroller and the external chip of SRAM.
I did and all worked as expected.

Serial Number Detection with openCV

I have a video with a serial number. Like in the picture. How I can with openCV detect the position of this patron. The only thing that I need is detect the location of this patron. Always this patron will have 12 numbers and will be white.
example
using Morphological Transformations you can find location of numbers.
try the code below (not a perfect code, it is just for instruction)
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src=imread("dnpaP.jpg");
Mat thresh = src.clone();
dilate(thresh,thresh,Mat(),Point(-1,-1), 5);
erode(thresh,thresh,Mat(),Point(-1,-1), 5);
cvtColor(thresh, thresh, COLOR_BGR2GRAY);
threshold(thresh, thresh, 200, 255, THRESH_BINARY);
erode(thresh,thresh,Mat(),Point(-1,-1), 3);
dilate(thresh,thresh,Mat(),Point(-1,-1), 3);
vector<vector<Point> > contours;
findContours(thresh.clone(), contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
for( size_t i = 0; i< contours.size(); i++ )
{
Rect boundingRect_ = boundingRect( contours[i] );
if(boundingRect_.width > boundingRect_.height * 12)
rectangle(src,boundingRect_,Scalar(0,0,255),2);
}
imshow("thresh",thresh);
imshow("src",src);
waitKey();
}

How to load multiple images as command line arguments to sprintf() and cvLoadImage() in OpenCV...?

This is the code i have., which will detect the faces in the image and draws rectangle around the face.
CvHaarClassifierCascade *cascade;
CvMemStorage *storage;
void detectFaces (IplImage *newframe);
int key;
int iImg;
int Num_Images=20;
int main( int argc, char** argv )
{
string filename = M:/Trend/FaceDetection/MyCascadeFolder/haarcascade_frontalface_alt2.xml";
storage = cvCreateMemStorage(0) ;
cascade = ( CvHaarClassifierCascade* )cvLoad( filename.c_str());
**//to load a single image**
**IplImage* inImage = cvLoadImage("M:/Trend/FaceDetection775/MyImages/face1.jpg",1);**
//IplImage* inImage = cvLoadImage(argv[1],1); >> It also works fine through command line
detectFaces( inImage );
cvShowImage("FaceDetection2",inImage);
cvSaveImage("M:/FaceDetection/MyImages/OpImages/faces.jpg",inImage);
cvReleaseImage( &inImage);
// **to load no. of images or a complete folder**
char buf[20];
for(iImg=0; iImg<=Num_Images; iImg++)
{
**sprintf(buf, "M:/FaceDetection/ImagesFolder/P%04d.jpg", iImg);**
*// sprintf(buf, argv[2], iImg); // I Tried with this., but its not working*
printf("\n");
inImage = cvLoadImage(buf, CV_LOAD_IMAGE_UNCHANGED);
printf("Input Image = P%04d.jpg\n", iImg);
detectFaces( inImage );
cvShowImage("FaceDetection",inImage);
cvReleaseImage( &inImage );
key= cvWaitKey(0);
if ( key == 'q' || key == 'Q' )
return(0);
}
cvReleaseHaarClassifierCascade( &cascade );
cvReleaseMemStorage( &storage );
return 0;
}
**// the actual detectfaces() function goes like this**
void detectFaces( IplImage *newframe)
{
CvSeq *faces = cvHaarDetectObjects( newframe, cascade, storage, 1.15, 5, 0, cvSize( 30, 30 ) );
for( int i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ )
{
CvRect *r = ( CvRect *)cvGetSeqElem( faces, i );
cvRectangle(newframe,cvPoint( r->x, r->y ),cvPoint( r->x + r->width, r->y + r->height ),CV_RGB( 0, 255, 0 ), 2, 8, 0 );
}
}
Here., sprintf() method is taking multiple images statically (within the program itself). But I want to pass those multiple images as a single folder through command line argument.
I try to use cvLoadImage() function, but it also takes only one image as input instead of a folder.
Also I am not able to count the number of rectangles it draws for every single face in the image., how can I do that also...?
Please help me in this regards.
I am working on face detection project "www.MahdiRezaei.com" for my academics., and I am using visual studio-2010 with C, C++ and OpenCV.
Thankz in advance.
You can change the following code to use it with arguments.
#include<Windows.h>
#include<iostream>
#include<opencv2\highgui\highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
WIN32_FIND_DATA data;
HANDLE h = FindFirstFile(L"c:\\Images\\*.*", &data); // first file is the current directory
FindNextFile(h, &data); //second file is the parent directory
vector<Mat> Images;
if (h != INVALID_HANDLE_VALUE)
{
while (FindNextFile(h, &data))
{
string filename = data.cFileName;
sprintf(filename, "%s%s","C:\\Images\\", filename.c_str());
Images.push_back(imread(filename, 1));
delete[] nPtr;
}
}
else
cout << "Error: No such folder." << endl;
FindClose(h);
return 0;
}

How to directly query the camera about image luminance/ Skip compentation in OpenCV

I am developing a program in VS 2010 using OpenCV. I want to measure the luminance of every frame that the computer's camera captures. However, the camera's software stabilizes the luminance after 2-3 frames. Eg, if i put my thumb in front of the camera the first frame's luminance is 2 (scale from 0 to 255), but then while keeping my thumb in front of the camera the luminance becomes 7 and the 20 - it is stabilized there for the next frames. So the camera tries to make too dark pictures brighter and too bright pictures darker.
How can i measure the actual luminance without the camera's interference?
My code is:
#ifdef _CH_
#pragma package <opencv>
#endif
#include "stdafx.h"
#include <highgui.h>
#include "cv.h"
#include <stdio.h>
#include <stdlib.h>
#include "..\utilities.h"
int _tmain(int argc, _TCHAR* argv[])
{
FILE *file;
IplImage *img;
IplImage* grayscale_image;
int c, i, j, Luminance = 0, Pixel_Num = 0;
int Avg_Luminance;
int width_step;
int pixel_step;
// allocate memory for an image
// capture from video device #1
CvCapture* capture;
// create a window to display the images
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
// position the window
cvMoveWindow("mainWin", 5, 5);
while(1)
{
if(file = fopen("luminance_value.txt", "w+"))
{
// retrieve the captured frame
capture= cvCaptureFromCAM(1);
img=cvQueryFrame(capture);
grayscale_image = cvCreateImage( cvGetSize(img), 8, 1 );
cvConvertImage( img, grayscale_image );
width_step= grayscale_image->widthStep;
pixel_step= grayscale_image->widthStep/grayscale_image->width;
Pixel_Num = grayscale_image->width * grayscale_image->height;
for(i = 0; i < grayscale_image->height; i++)
{
for(j = 0; j < grayscale_image->width; j++)
{
unsigned char* point = GETPIXELPTRMACRO( grayscale_image, j, i, width_step, pixel_step);
Luminance += point[0];
}
}
Avg_Luminance = Luminance / Pixel_Num;
//Avg_Luminance = cvGetCaptureProperty(capture,CV_CAP_PROP_BRIGHTNESS);
//file = fopen("luminance_value.txt", "w+");
fprintf(file, "%d", Avg_Luminance);
fclose(file);
printf("Avg_Luminance = %d\n", Avg_Luminance);
Luminance = 0;
Pixel_Num = 0;
// show the image in the window
cvShowImage("mainWin", grayscale_image );
cvReleaseCapture(&capture);
// wait 10 ms for a key to be pressed
c=cvWaitKey(10000);
// escape key terminates program
if(c == 27)
break;
}
else
{
continue;
}
}
return 0;
}

Resources