Ask Your Question
1

Create a two channels blob for dnn::Net

asked 2017-10-01 01:14:34 -0600

tham gravatar image

updated 2017-10-01 17:45:20 -0600

I want to pass 2 dimension channels into the dnn::Net, but blobFromImage do not support it, so I try to do it by myself

int sz[] = { 1, 2, 128, 128};
cv::Mat blob = Mat(4, sz, CV_32F);

cv::Mat img1 = cv::imread("left_img.jpg", IMREAD_GRAYSCALE);
cv::Mat img2 = cv::imread("right_img.jpg", IMREAD_GRAYSCALE);
img1.copyTo(Mat(img.rows, img.cols, CV_32F, blob.ptr((0, 0))));
img2.copyTo(Mat(img.rows, img.cols, CV_32F, blob.ptr((0, 1))));
net.setInput(blob, "input"); //set the network input
Mat result = net.forward(); //compute output

However, this give me error

tensorflow_model_loader': malloc(): smallbin double linked list corrupted: 0x00000000024d1490 ***

What is the correct way of passing two channels blob into dnn::Net?

Edit :

Two bugs, 1 of them pointed out by berak(thanks), another is due to the extra "()", should change blob.ptr((0, 0)) to blob.ptr(0, 0)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-10-01 01:29:59 -0600

berak gravatar image

updated 2017-10-01 18:22:30 -0600

you're copying a CV_8U image to a CV_32F one:

img1.copyTo(Mat(img.rows, img.cols, CV_32F, blob.ptr((0, 0))));

i guess, you have to convert the input manually to float, before copying it:

img1.convertTo(img1, CV_32F); // does it need scaling ? idk.
img1.copyTo(Mat(img1.rows, img1.cols, CV_32F, blob.ptr(0, 0)));

or, even shorter:

img1.convertTo(Mat(img1.rows, img1.cols, CV_32F, blob.ptr(0, 0)), CV_32F);
edit flag offensive delete link more

Comments

Thanks, unless I can forward the data now. There are another bug too, I edited in my post

tham gravatar imagetham ( 2017-10-01 17:44:17 -0600 )edit

Is it a good idea to add an api called blobFromNDimImage?

tham gravatar imagetham ( 2017-10-01 17:51:56 -0600 )edit

ah, yea, it's blob.ptr(imgnum, channelnum) ( with no extra braces).

and, if you want my opinion - no, no extra function nessecary. maybe we only have to convince @dkurt et all, that using stereo images here (2 channels, never used anywhere else in opencv) is a legit use-case !)

berak gravatar imageberak ( 2017-10-01 18:28:45 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-10-01 01:14:34 -0600

Seen: 743 times

Last updated: Oct 01 '17