Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
 #pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
//Llibreries visió
#include <cv.h>
#include <highgui.h>
#include <cvcompat.h>
//

//Llibreries de windiws 32
#include <windows.h>
#include <winbase.h>

int main()
{
   //variables visió
  IplImage *img = 0;
  IplImage *monoImg = 0, *resultat = 0, *dst = 0;
  int height, width, step;
  uchar * data;
  int stepResultat;
  uchar * dataResultat;
  int i, j, k;

  float POS_X;
  int CON_1;
  int percent=50;  //Tant per cent de reducció de la imatge que volem
  //Variables comunicació RS232
  unsigned char SENTIT[4];
   unsigned short int rpm;
   HANDLE hSerial;
   DCB dcbSerialParams = {0};
   COMMTIMEOUTS timeouts = {0};
   DWORD dwBytesTrans = 0;

  //Acquireixo imatges de la web cam
  cvSize(10,10);
  strcpy(SENTIT,"012");
  CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
  if( !capture )
  {
    fprintf( stderr, "ERROR: capture is NULL \n" );
    getchar();
    return -1;
  }
  img = cvQueryFrame( capture );
  if( !img )
  {
    fprintf( stderr, "ERROR: frame is null...\n" );
    getchar();
    return -1;
  }
  //PROVES
  //Encongeix l'imatge
       /* Create destination image */
	  dst = cvCreateImage(
	      cvSize(
	        img->width * percent / 100,
	        img->height * percent / 100),
	        img->depth,
	        img->nChannels
	  );

	  cvResize(img, dst, CV_INTER_LINEAR);
  //PROVES

  //Allocate space for the result image
  monoImg = cvCreateImage (cvGetSize (dst), 8, 1);
  resultat = cvCreateImage (cvGetSize (dst), 8, 1);
  // Create a window in which the captured images will be presented
  cvNamedWindow( "original1", CV_WINDOW_AUTOSIZE );
  cvNamedWindow( "resultat", CV_WINDOW_AUTOSIZE );
  // get the image data
  height = monoImg->height;
  width = monoImg->width;
  step = monoImg->widthStep/sizeof(uchar);
  data = (uchar *)monoImg->imageData;
  // get the image data
  stepResultat = resultat->widthStep/sizeof(uchar);
  dataResultat = (uchar *)resultat->imageData;
  //Configuro la comunicació série
   hSerial = CreateFile("COM4",
                   GENERIC_READ | GENERIC_WRITE,
                   0,
                   0,
                   OPEN_EXISTING,
                   FILE_ATTRIBUTE_NORMAL,
                   0);

   if (hSerial == INVALID_HANDLE_VALUE)
   {
      if (GetLastError() == ERROR_FILE_NOT_FOUND)
      {
         printf("Serial port does not exist.\n");
      }
      else
      {
         printf("Other error.\n");
      }
      exit(1);
   }

   dcbSerialParams.DCBlength = sizeof(dcbSerialParams);

   if (!GetCommState(hSerial, &dcbSerialParams))
   {
      printf("Error getting serial port state.\n");
   }

   //valid baud rate. Must be 9600, 19200, 38400 or 57600
   dcbSerialParams.BaudRate = CBR_9600;
   dcbSerialParams.ByteSize = 8;
   dcbSerialParams.StopBits = ONESTOPBIT;
   dcbSerialParams.Parity = NOPARITY;

   if (!SetCommState(hSerial, &dcbSerialParams))
   {
      printf("Error setting serial port state.\n");
   }

   timeouts.ReadIntervalTimeout = 0;
   timeouts.ReadTotalTimeoutConstant = 250;
   timeouts.ReadTotalTimeoutMultiplier = 1;
   timeouts.WriteTotalTimeoutConstant = 25;
   timeouts.WriteTotalTimeoutMultiplier = 1;

   if (!SetCommTimeouts(hSerial, &timeouts))
   {
      printf("Error setting timeouts.\n");
   }
  
  // Show the image captured from the camera in the window and repeat
  while( 1 )
  {
    img = cvQueryFrame( capture );
    //Encongeix l'imatge
       /* Create destination image */
	  IplImage* dst = cvCreateImage(
	      cvSize(
	        img->width * percent / 100,
	        img->height * percent / 100),
	        img->depth,
	        img->nChannels
	  );

	  cvResize(img, dst, CV_INTER_LINEAR);
    //Fi encongeix
    cvCvtColor(dst,monoImg,CV_RGB2GRAY);
    //j
    clrscr();
    //  Envio "HOLA" pel port série
    /*
    if (!WriteFile(hSerial, "HOLA", 4, &dwBytesTrans, NULL))
    {
      printf("Error sending interactive command.\n");
    }
    */
    i= height/2;//Faig un escombrat horitzontal a la posició central

        // j
        gotoxy(35,10);
        //Envio valor posició línia **************************
        if (CON_1!=0)
        {
                rpm=(unsigned  int)((POS_X/CON_1)*255/width);
                gotoxy(37,12);
                printf("%i",rpm);

                /*
                if (!WriteFile(hSerial, (unsigned char *)(&rpm)+1, 1, &dwBytesTrans, NULL))
                {
                        printf("Error sending MSB RPM.\n");
                }

                // Send the  LSB RPM
                if (!WriteFile(hSerial, &rpm, 1, &dwBytesTrans, NULL))
                {
                      printf("Error sending LSB RPM.\n");
                }
                */

                if(rpm<102)     //envio 0 pel pot serie
                {
                        if (!WriteFile(hSerial,(unsigned char *)(&SENTIT), 1, &dwBytesTrans, NULL))
                        {
                                printf("Error sending LSB RPM.\n");
                        }
                }
                else if ((rpm>=102)&&(rpm<=153))    //envio 1 pel port serie
                {
                        if (!WriteFile(hSerial,(unsigned char *)(&SENTIT)+1, 1, &dwBytesTrans, NULL))
                        {
                                printf("Error sending LSB RPM.\n");
                        }
                }
                else if(rpm>153)    //Envio 2 pel port série
                {
                        if (!WriteFile(hSerial, (unsigned char *)(&SENTIT)+2, 1, &dwBytesTrans, NULL))
                        {
                                printf("Error sending LSB RPM.\n");
                        }
                }
        }
        POS_X=0;
        CON_1=0;
        // j
        for(j=0;j<width;j++)>
        {

                if (data[i*step+j] > 200)    //125
                {
                        dataResultat[i*stepResultat+j]=255;
                        //J blanc
                        CON_1++;
                        POS_X=POS_X+j;
                }
                else
                {
                        dataResultat[i*stepResultat+j]=0;
                        //J Negre

                }
      }
   
        cvShowImage( "original1", monoImg );
    cvShowImage( "resultat", resultat );

    if( (cvWaitKey(10) & 255) == 27 ) break; //Prem tecla ESC per sortir
  }

  // Release the capture device housekeeping
  cvReleaseCapture( &capture );
  cvDestroyWindow( "original" );
  cvDestroyWindow( "resultat" );
  cvReleaseImage(&monoImg);
  cvReleaseImage(&resultat);
  //Tanca el fitxer de la comunicació série
  CloseHandle(hSerial);
  return 0;
}
Posted
Updated 2-Nov-11 17:48pm
v2
Comments
Richard MacCutchan 2-Nov-11 18:35pm    
You need to explain exactly what is happening here. People do not have the time to analyse your code to try and find out why it will not work.
Albert Holguin 2-Nov-11 20:13pm    
Provide more information as to what you mean by "doesn't work".

1 solution

First, see the comment to your question by Richard.

As a next step, try to run the application run as administrator. Logging in as administrator is not enough. Here is how you do it: http://www.sevenforums.com/tutorials/11841-run-administrator.html[^].

Try it and report back what happened. Run the code under debugger and tell where exactly was the exception, mark this line(s) in the code and dump all exception information. Use "Improve question" above.

—SA
 
Share this answer
 
Comments
Albert Holguin 2-Nov-11 20:12pm    
Great advice, five'd
Sergey Alexandrovich Kryukov 2-Nov-11 20:25pm    
Thank you, Albert.
--SA
Member 7837185 3-Nov-11 11:53am    
I,ve tried to run as administrator but I still can't obtain image from web cam. Tre program doesn't give any error. But the window were it should appear the image capturated from web cam stays black.

Thank you
Sergey Alexandrovich Kryukov 3-Nov-11 17:59pm    
OK, but you can also run it under debugger and see what's failing. What do you mean "error"? Do you catch all exceptions and show exception information? You need to.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900