Ask Your Question
0

Showing gray image in picturebox in Visual studio c++

asked 2017-02-16 04:15:39 -0600

BigC gravatar image

Hi all, I am trying to do some gui to show original image, gray, blur, and other things with visual studio c++ WinForms. I wanted to show the mat images in picturebox and I found some examples to do it. I can show images in picturebox now. However when I try to shown gray scale image in picture box, I get different color additional to gray image. I see different colors. I also open gray image with imshow, There is no problem with image. I think conversion keeps other 2 channels. I have searched very long time but I could not find any answers for my problem.

I tried to add a link which shows result of my gui. However my karma is not enough to share link

all Suggestions are appreciated.

System::Drawing::Image^ OpencvReadPicture() {
    cv::Mat originalImage ;
    originalImage = cv::imread("banana.jpg", cv::ImreadModes::IMREAD_COLOR);
    cv::imshow("OriginalImage",originalImage);      
    System::Drawing::Bitmap^ bitmap = gcnew System::Drawing::Bitmap(originalImage.cols, originalImage.rows, originalImage.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, System::IntPtr::IntPtr(originalImage.data));
    System::Drawing::Image^  image = static_cast<System::Drawing::Bitmap^>(bitmap);     
    return image ;
};
System::Drawing::Image^ OpencvGrayPicture() {
    cv::Mat grayImage;      
    cv::cvtColor(originalImage, grayImage, CV_BGR2GRAY,1);      
    cv::imshow("GrayImage", grayImage);     
    System::Drawing::Bitmap^ bitmapGray = gcnew System::Drawing::Bitmap(grayImage.cols, grayImage.rows, grayImage.step, System::Drawing::Imaging::PixelFormat::Format8bppIndexed, System::IntPtr::IntPtr(grayImage.data));
    System::Drawing::Image^  imageGray = static_cast<System::Drawing::Bitmap^>(bitmapGray);
    return imageGray;
};
pBoxOriginalImage->Image =OpencvReadPicture();// showing original image in the picture box.
pBoxGrayImage->Image = OpencvGrayPicture();// showing gray image in the picturebox.
edit retag flag offensive close merge delete

Comments

Format8bppIndexed <-- that's not grayscale (also, afaik, that picturebox does not have an 8bit grayscale mode)

berak gravatar imageberak ( 2017-02-16 04:28:24 -0600 )edit

when I load manually the saved gray image in the picturebox, Format8bppIndexed is show as pixel format. so what is the solution to show grayscale in picturebox?

BigC gravatar imageBigC ( 2017-02-16 04:32:51 -0600 )edit
1

yes, Format8bppIndexed is a pixel format, but the wrong one.

afaik, there is only 16bit grayscale supported there, so you'd have to convert your image, and use the correct flag (idk.)

berak gravatar imageberak ( 2017-02-16 04:45:07 -0600 )edit

I tried all pixel format. If I use Format24bppRgb, I get gray image but I see 3 images in a pixturebox,

BigC gravatar imageBigC ( 2017-02-16 06:08:08 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-02-17 14:00:39 -0600

Guyygarty gravatar image

I have not tried to put a grayscale image directly into the picturebox but rather converted the gray image to an RGB image and then put that. something like:

cvtColor(GrayImage, ColorImage, CV_GRAY2RGB, 3);
 System::Drawing::Bitmap^ BitmapToDisplay=gcnew System::Drawing::Bitmap(ColorImage.cols, ColorImage.rows, (int)ColorImage.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ColorImage.ptr());

guy

edit flag offensive delete link more

Comments

It is done. I used cv_gray2bgr , it worked thanks

BigC gravatar imageBigC ( 2017-02-18 11:53:13 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-02-16 04:15:39 -0600

Seen: 2,477 times

Last updated: Feb 17 '17