0

i am writing an opencv program, changing the image color and saving the image with a name given by the user, that name is stored at some variable. I want to use popen to send that file to print.

#include <stdio.h>
#include "cv.h"
#include "highgui.h"

int main (int argc, char ** argv)
{
    int p[3];
    p[0] = CV_IMWRITE_JPEG_QUALITY;
    p[1] = 10;
    p[2] = 0;
    char name[10];
    IplImage* in = cvLoadImage("imagenoise1.jpg",3);
    IplImage* gray = cvCreateImage( cvSize(in->width, in->height),IPL_DEPTH_8U, 1 );
    cvCvtColor( in, gray, CV_BGR2GRAY );

    cvNamedWindow("in", CV_WINDOW_AUTOSIZE );
    cvNamedWindow("gray", CV_WINDOW_AUTOSIZE );
    cvShowImage("in",in);
    cvShowImage("gray",gray);
    printf("Con que nombre desea guardar la imagen en escala de grises ( no mas de 9 caracteres ):\n");
    scanf("%s", name);
    cvSaveImage( name, gray, p );
     FILE *pipe = popen("lp name","w");
    close(pipe);
    cvWaitKey(0);
    cvDestroyWindow( "in" );
    cvDestroyWindow( "gray" );
    cvReleaseImage( &in );
    cvReleaseImage( &gray );



    return 0;
}

So i am trying to send the specific name i gives before, but nothing seems to work, any idea?, i think i am missing something because send a file that does not exist.

mrmurmanks
  • 33
  • 12

2 Answers2

0

You try to open imagenoise1.jpg which is not given by user and maybe that's why you receive FILE_NOT_FOUND exception.

IplImage* in = cvLoadImage("imagenoise1.jpg",3);

I think you should change your code in this way:

IplImage* in = cvLoadImage(argv[1], 3);

And you should run/debug your application by passing parameter: test.exe imagenoise.jpg

Mustafa Chelik
  • 2,113
  • 2
  • 17
  • 29
0

This code is suspicious to me:

scanf("%s", name);
cvSaveImage( name, gray, p );
FILE *pipe = popen("lp name","w");

It looks as though you need to do:

if (scanf("%s", name) == 1)
{
    char cmd[256];
    snprintf(cmd, sizeof(cmd), "lp %s", name);
    system(cmd);
}

You neither write anything to the pipe nor read anything from it, so you don't need popen() and system() should work just as well if not better.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278