Ask Your Question
0

Assgin value to cv::Mat using Mat.at()

asked 2018-03-13 05:24:16 -0600

open_ranger gravatar image

updated 2018-03-13 05:24:58 -0600

I'm trying to rearrang Mat imageWork which is 800 x 600 Type:8UC3 into kmeansData which is 480000 x 1 Type:32FC3.The Vec3f pixel can fetch vaule correctly but at the last step nothing is copied to kmeansData.In the debbuger it shows kmeansData.data=uchar*='/0'

    kmeansData = Mat(imageWork.rows * imageWork.cols, 1, CV_32FC3);
    for (int x = 0; x < imageWork.rows; x++) {
        for (int y = 0; y < imageWork.cols; y++) {
                Vec3f pixel=(Vec3f)imageWork.at<Vec3b>(x, y);
                kmeansData.at<Vec3f>(y + x * imageWork.cols,0)= pixel;
        }
    }

Anybody can explain to me why?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-03-13 05:51:15 -0600

berak gravatar image

updated 2018-03-14 02:50:21 -0600

  • please NEVER do anything per pixel ! (can't be said often enough)
  • that opencv has it the rows/cols way is bad enough, but changing the sematics of x or y like x < imageWork.rows makes it only worse.
  • you don't need to copy anything here, a reshape() will do:


Mat kmeansData = imageWork.reshape(1, imageWork.total()); // 1 channel, 3 cols, H*W rows
edit flag offensive delete link more

Comments

"please NEVER do anything per pixel ! (can't be said often enough)" :D I think you can add this to our faq page itself :P

Balaji R gravatar imageBalaji R ( 2018-03-13 06:30:18 -0600 )edit
1
berak gravatar imageberak ( 2018-03-13 06:34:20 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-03-13 05:24:16 -0600

Seen: 641 times

Last updated: Mar 14 '18