Ask Your Question
0

Initialize MAT from pointer - help. [closed]

asked 2017-03-16 06:13:04 -0600

Soarez gravatar image

Hi, Im working with a camera, and the camera gives me the image in its own type of variable, that contains image width , height , depth , and other things.

In this data i receive a pointer for the first pixel , and i know that the RGB is organized like (assuming the image as n pixels), the first n address have the R, the next n address have the G and the last n address have the B.

So im trying to initialize the image like this.

cv::Mat dummy_query = cv::Mat(img->h, img->w, CV_8UC3, pData , 0 );

pData is the pointer from the first pixel. the 0 ,on openCV doc they say its the step , i really dont understand what is this step.

whit this i get some data to the matrix, but when i do the imshow nothing happens.

hope someone can help. =)

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by berak
close date 2017-03-16 12:43:39.657558

2 answers

Sort by ยป oldest newest most voted
2

answered 2017-03-16 06:28:35 -0600

berak gravatar image

updated 2017-03-16 06:35:04 -0600

if your data comes as consecutive image "planes" you will need 3 seperate 1 channel Mat's with the color channels, and then merge them (we also need to swap b<-->r for opencv):

Mat chn[] = {
    Mat(img->h, img->w, CV_8UC1, pData + img->h*img->w*2),  // starting at 1st blue pixel 
    Mat(img->h, img->w, CV_8UC1, pData + img->h*img->w),    // 1st green pixel
    Mat(img->h, img->w, CV_8UC1, pData)                     // 1st red pixel
};

Mat bgr;
merge(chn,3,bgr);
imshow("my image !", bgr);
waitKey(33); // else it won't show anything
edit flag offensive delete link more

Comments

Looks like i have a 3 channel image now ,i print one col ...

although i cant see the image on imshow, but this could be other problem.

Soarez gravatar imageSoarez ( 2017-03-16 06:42:25 -0600 )edit

if you can't get it done - show us your attempt !

berak gravatar imageberak ( 2017-03-16 06:49:33 -0600 )edit

im just doing

namedWindow( "Display windsssow", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display windsssow",bgr);
cv::waitKey(3);

it should work =)

Soarez gravatar imageSoarez ( 2017-03-16 06:51:31 -0600 )edit

@berak , Im working on a thread, and on the main i can load images but on the thread i can't, and the same with display and imwrite.

Soarez gravatar imageSoarez ( 2017-03-16 07:17:19 -0600 )edit

yea, bad idea.

berak gravatar imageberak ( 2017-03-16 07:19:18 -0600 )edit

@berak so i cant do it on the thread?

Soarez gravatar imageSoarez ( 2017-03-16 07:29:08 -0600 )edit

at least you should not try to use gui functions in a thread

berak gravatar imageberak ( 2017-03-16 07:47:56 -0600 )edit

im getting images like this ...link text , do you have any idea why!?
http://imgur.com/a/8bXEG

Soarez gravatar imageSoarez ( 2017-03-16 09:02:17 -0600 )edit

well, that's definitely not correct. maybe your assumption about the input buffer was wrong ?

berak gravatar imageberak ( 2017-03-16 09:07:54 -0600 )edit

yeah, i already asked the seller waiting for response. Do you know what padding bytes mean!? for each image i recive a x_padding and y_padding, but i dont know what they mean.

Soarez gravatar imageSoarez ( 2017-03-16 09:17:11 -0600 )edit

oh, useful hint !

padding means, that additional bytes are added at the end of each row (e.g. to align them to multiples of 4 bytes), and indeed, your image looks, like it is suffering from that.

unfortunately, this will further complicate the copying, since now, you have to do it per row, not per channel/plane.

do you have the exact values for x_padding and y_padding ? show us ! (then we can think about a remedy)

berak gravatar imageberak ( 2017-03-16 09:22:01 -0600 )edit

just one more thing @berak , and already thanks a lot for your help, if i have a image that come in like address 1 - pixel 1 blue address 2 - pixel 1 reg address 3 - pixel 1 green address 4 - pixel 2 blue address 5 - pixel 2 reg address 6 - pixel 2 green and so on , how do i initialise the mat!?

Soarez gravatar imageSoarez ( 2017-03-16 09:23:50 -0600 )edit

yes i printed it, padding values are 0 , so no problem =).

Soarez gravatar imageSoarez ( 2017-03-16 09:25:34 -0600 )edit

^^ not sure, if i understood the latter, can you try again ? maybe a little ascii art or drawing ? or numbers ?

berak gravatar imageberak ( 2017-03-16 09:26:24 -0600 )edit

OK, i think that i have a separated planar image like you said on your answer. But what if i have a mixed memory where the 3 first address are for pixel one, the next 3 for pixel 2, the next 3 for pixel 3 and so one. see on the link http://imgur.com/a/19Ma2 , red green and blue order are just for the example.http://imgur.com/a/19Ma2 (img)

Soarez gravatar imageSoarez ( 2017-03-16 09:45:38 -0600 )edit
0

answered 2017-03-16 12:40:55 -0600

Soarez gravatar image

@berak , its working , used the same think that i used on the question , because the images is 8bit packed RGB, so im sorry for your time , but thanks again =)

edit flag offensive delete link more

Comments

no worries ;)

berak gravatar imageberak ( 2017-03-16 12:43:58 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-03-16 06:13:04 -0600

Seen: 2,241 times

Last updated: Mar 16 '17