Is it possible for me to typecast or convert a type IplImage* to const char* It seems impossible, but i would like to send a frame captured from webcam through a winsock socket directly through memory but the 'SEND' function only accepts type const char *. Ive tried type casting to no avail.

If it's not possible, what should i do ?

You can always typecast to "char*" just use

reinterpret_cast<char*>(myObject)

After that you'll just need to know the exact size that object takes in memory to send it over winsock, try

sizeof(myObject)

Or you could use the classs variable IplImage->imageData which is a char pointer to the inline-data of the imageData.

I think your solution worked! Ill check and let you know!

It works but how would i go abouts putting it into an IplImage buffer on the other end?

I compiled everything great, but my program crashes.
It also asks me for a video source before crashing.
Here are the relevent server/client codes. Note that it is programmed so the SERVER sends the image to the CLIENT.


Client:

client=accept(kSock, (sockaddr*)&sin, &len);
std::cout<<"Connection established!\n";
while (recv(kSock, buf, sizeof(buf), 0))

{
      
       IplImage *frame = cvCreateImage(cvSize(500, 500), 8, 3);
       frame->imageData = (char *)buf;
       cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
	   cvShowImage( "result", frame );

}
return 0;
}

Server:

char StartStream(){
     
      CvCapture *capture = 0;
    IplImage  *frame = 0;
    int       key = 0;
        capture = cvCaptureFromCAM( 0 );
 
 
 
    /* always check */
 
    if ( !capture ) {
 
        fprintf( stderr, "Cannot open initialize webcam!\n" );
 frame = cvQueryFrame( capture );
        return *frame->imageData;
 
	}

}

...

send(kSock,(const char*)StartStream(), sizeof(StartStream()), 0);

The connection goes through so the crash is 100% Open CV related.

sizeof(StartStream())

? really ? :)

if there's a variable giving you the byte stream, maybe there's on that gives you the size of that stream, use that :)

But I realyl think you should reconsider using

reinterpret_cast

The thing about that one is that you can send the entire object (not just the data bytes) and you'd just reinterpret_cast it from char* to your object type when you get it on the client side !

If it says that it can't cast it ... please paste the code here so I can take a peak at it :)

Reinterpret cast says the conversion is impossible return reinterpret_cast<char*>(frame);

frame is a pointer, right ?

Stuff like this works:

CObject * pObj;
    char * pRaw = reinterpret_cast<char*>(pObj);

But if you have a simple object you need to give it's address to the cast as:

reinterpret_cast<char*>(&obj)

Warning: Don't try to convert local objects and return the pointer to them (as I think you're doing). The object goes out of scope at the end of the function and using the converted pointer will crash!

You could paste some more relevant code so I can see what you're doing.

I totally disregarded the rules of scope! Ill make modifications and let you know! Thanks!

IplImage *frame = 0; Put in global scope. char StartStream(IplImage * pImg); Function modified to take a pointer. That pointer is then corverted and returned in the same manner

char StartStream(IplImage * pImg){
     
     CvCapture *capture = 0;
    
    int       key = 0;
        capture = cvCaptureFromCAM( -1 );
 
 
 
    /* always check */
 
    if ( !capture ) {
 
        fprintf( stderr, "Cannot open initialize webcam!\n" );
 pImg = cvQueryFrame( capture );
        return *pImg->imageData;
 
	}

}
..
send(kSock,(const char*)StartStream(frame), sizeof(StartStream(frame)), 0);

Note that both applications are crashing.

. Still get a crash. Ill try your other advise.

They crash because this is not ok:

sizeof(StartStream(frame))

That piece of code REALLY doesn't do what you think it does. it will ALWAYS return 1 = the size of char!
Besides, you need to return char* from that function;

Actually all you really need there is:

IplImage * frame;
    send(kSock, reinterpret_cast<char*>(frame), sizeof(IplImage), 0);

EDIT:
Actually I'm wrong :)

sizeof(IplImage) isn't enough as this object has pointers and you'd need to add the size of the data they point to ... and the data ...

DON"T do this as it wrong if your object has pointers in it ...
Sorry about that

sigh..

made a new function

int streamSize(IplImage * pImg){
	if(pImg!=0){
		return sizeof(*pImg->imageData);
	}

}

and then did this send(kSock,(const char*)StartStream(frame), streamSize(frame), 0); crash.

Sorry, but you did it wrong again :)

There is absolutely NO way of finding out the size of a byte stream in C++ except if the one that gives it to you tells you it's size ...

( You may think about strings and how you can use stuff like "strlen" to find it's size, but that only because of the convention that a char* string ends in a '\0', this is not the case for a data stream, where '\0' may be a valid piece of data)

You should look at the IplImage class for a member / method that gives you the size; I'm not familiar with the class but it should be something like imageDataSize / imageDatalength / GetDataSize() / ... you get the idea :)

sizeof(*pImg->imageData);

will always return 1 (that is the size of byte, which I'm assuming it's that member's type)

This article has been dead for over six months. Start a new discussion instead.