I am trying to capture frames from a Logitech HD cam connected to a Raspberry Pi through usb, the RP is running arch linux and I am using OpenCV C api and a TCP client.
The TCP server is running c++(QT) under ubuntu.
here is my client.c code
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc,char *argv[])
{
int sockfd,portno,n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[999999];
if(argc <3)
{
fprintf(stderr,"usage %s hostname portname port\n",argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET , SOCK_STREAM,0);
if(sockfd < 0)
{
error("ERROR OPENING SOCKET");
}
server = gethostbyname(argv[1]);
if(server == NULL)
{
fprintf(stderr,"ERROR,NO SUCH HOST\n");
exit(0);
}
bzero((char*)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char*)server->h_addr,(char*)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(portno);
if(connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
{
error("ERROR CONNECTING");
}
CvCapture *capture = cvCaptureFromCAM(1);
// capture from cam
int i =1;
while ( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
if ( !frame ) {
//fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}else
{
i++;
}
bzero (buffer,999999);
strcpy(buffer,frame->imageData);
n=write(sockfd,buffer,strlen(buffer));
if(n <0)
{
error("ERROR READING FROM SOCKET");
}
//printf("%s\n",buffer);
}
return 0;
}
This is how I receive data at the server:
void HostConnector::readyRead()
{
QByteArray Data = socket->readAll();
IplImage* frame = new IplImage();
frame->imageData = Data.data();
cvShowImage( "mywindow", frame ); //show the frame in a window
}
But I am receiving this error:
OpenCV Error: Bad flag (parameter or structure field) (unrecognized or
unusupported array type) in cvGetMat, file
/home/kato/GP/src/OpenCV-2.4.2/modules/core/src/array.cpp,line 2482Qt has caught an exception thrown from an event handler. Throwing
exceptions from an event handler is not supported in Qt.You must
reimplement QApplication::notify() and catch all exceptions thereterminate called after throwing an instance of
‘cv::Exception’,error:(-206) Unrecognized or unsupported array type in
function cvGetMat.
Does anyone knows how to solve this issue ??
Thanks in advance.
I think that the problem is that you are using
strlen(buffer)– this function returns position of first'\0'byte, not length of buffer. Try to copy the buffer using some function for memory copying(likememcpyon windows) and as buffer length useframe->imageSize. Don’t forget to set imageSize, width and height in your image on server. Generally i would recommend to send all important information(like image width, height, etc) before first frame and than use this header without changing it.