本文介绍了为什么此C应用程序在Windows XP中而不在Windows 7中工作,库openCV& api视窗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #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;
}

推荐答案


这篇关于为什么此C应用程序在Windows XP中而不在Windows 7中工作,库openCV&amp; api视窗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 09:55