C# winform picturebox image lost after saving data - c#-4.0

I have a winform application in which there is a pictureBox what displays a pretty big image (2550by4500). This bitmap image is transformed from a byte array using unsafe pointer, like this:
Bitmap img;
unsafe
{
fixed (Byte* intPtr = &outBuffer[0])
img = new Bitmap(_width, _height, _width * 3, System.Drawing.Imaging.PixelFormat.Format24bppRgb, new IntPtr(intPtr));
}
So far, no problems. After displaying the image, I saved the pixel values into a Matlab .mat file using this DLL (http://www.mathworks.com/matlabcentral/fileexchange/16319). Still, no problem with saving.
However, the image in the pictureBox became like a noisy black-white image, the original image was completely lost.
Things I tried:
added the Bitmap in watch window, found out pixel values all changed. Bitmap is corrupted.
do that unsafe transformation again everytime after saving, however, it brings another problem: "AccessViolationException in Drawing.dll".
Something must have to do with the .mat saving part, because if I skip saving, no problem at all. But I do not know how they are related, memory? I tried a smaller size image, no problem. so I'm assuming that "save .mat" process corrupted the Bitmap?
Any idea would be helpful! Thank you

Related

Converting PDF/SVG to PNG with antialias off using Magick++

I've tried to convert a SVG file to PNG with antialiasing off in Magick++ but I wasn't successful. But I was able to convert the SVG file to PDF with another program and the use the ImageMagick convert command to convert the PDF file to PNG.
How can I use ImageMagick to do it? The command I use for converting PDF to PNG is this:
convert +antialias -interpolate Nearest -filter point -resize 1000x1000 "img.pdf" PNG24:"filter.png"
Is there any way to use Magick++ to do that or better, convert SVG to PNG directly with antialiasing off?
Thanks in advance.
Edit:
The answer given in this post doesn't work for me. Possible because I'm using a colored SVG instead of 1-bit alpha channel. Also I mentioned in my question that I'm also looking for a way to do this in Magick++.
Magick++ has the Magick::Image::textAntiAlias & Magick::Image::strokeAntiAlias methods available, but they would only be useful if your parsing the SVG and rebuilding the image (i.e. roll-your-own SVG engine) one SVG element at a time.
As #ccprog pointed out in the comments; once the decoder utility rasters the vectors, the damage is done & setting the flags would not have an effect on the resulting resize.
Without seeing the SVG, I can only speculate what the problem is. I would suggest setting the document size before reading the SVG content.
For example, read the image at a smaller size than resample up.
Magick::Image img;
img.size(Magick::Geometry(100, 100)); // Decode to a small context
img.read("input.svg");
img.interpolate(Magick::NearestInterpolatePixel);
img.filterType(Magick::PointFilter);
img.resize(Magick::Geometry(600, 600));
img.write("PNG24:output#100x100.png");
Or render at larger size then the finial image.
Magick::Image img;
img.size(Magick::Geometry(1000, 1000)); // Decode to a larger context
img.read("input.svg");
img.interpolate(Magick::NearestInterpolatePixel);
img.filterType(Magick::PointFilter);
img.resize(Magick::Geometry(600, 600));
img.write("PNG24:output#1000x1000.png");
Update from comments
For Postscript (PDF) & True-Type antialiasing, you would set Magick::Image::textAntiAlias (or Magick::Image::antiAlias if using IM6) to false. Just ensure that the density is set to allow any overhead.
Magick::Image img;
img.density(Magick::Point(300));
if (MagickLibVersion < 0x700) {
img.antiAlias(false);
} else {
img.textAntiAlias(false);
}
img.interpolate(Magick::NearestInterpolatePixel);
img.filterType(Magick::PointFilter);
img.read("input.pdf");
img.resize(Magick::Geometry(1000, 1000));
img.write("PNG24:output.png");

picturebox image as a parameter of imread in opencv

is there any possibility to read the image from the picture box in vc++ using opencv imread?
am using the following code,
vector< Mat > vImg;
Mat rImg;
vImg.push_back(imread(pictureBox1->Image));
vImg.push_back(imread(pictureBox2->Image));
Stitcher stitcher = Stitcher::createDefault();
stitcher.stitch(vImg, rImg);
but am getting error
What you should do is initialize the Mat object from the PictureBox pixel data directly.
1) Here is an example and short explanation of how to create a Mat object from memory buffer
2) Here you can see how to access the pixel memory of the PictureBox, it is better to use LockBits to obtain the pointer to the memory and pass that in the constructor, but if you are new to all this you can also get it pixel by pixel.

how to correct the orientation of the image with ITK-VTK

I am a beginner in VTK ITK, I am trying to read a DICOM series with ITK and display with VTK but I had pictures upside down, I tried to read a single image (JPG) with ITK and visualuser with VTK it is the same problem, so I had the idea of ​​treating the image on photoshop ie I applied to the original image rotation (vertical symmetry of the work area) and I did the reading with ITK and display with VTK, the image is displayed in the correct orientation, infact ITK keeps the orientation of the image, but the problem is at VTK, it is which displays the image upside down, I searched all over the internet I have not found a solution or a method or not even an idea, I encountered the same problem in many forums but there is no response, I count on your help, I can not apply any image processing to find a solution to this problem.
Please Help! thank you in advance
Ideally you should re-orient your camera in VTK so that it is suited for medical image visualization. (The default camera in VTK uses the computer graphics conventions).
If you want a quick hack, you can copy-paste the following code in ITK:
FlipFilterType::Pointer flipperImage = FlipFilterType::New();
bool flipAxes[3] = { false, true, false };
flipperImage = FlipFilterType::New();
flipperImage->SetFlipAxes(flipAxes);
flipperImage->SetInput( image );
flipperImage->Update();
I use a rapid way to set the orientation:
imageActor->SetOrientation(180,0,0);
No need to add filter.
Here's an example of how I would do it. I'm not sure what classes you are using, so I cannot be specific.
vtkSmartPointer<vtkImageData> result = vtkSmartPointer<vtkIMageData>::New();
result->DeepCopy(YourImage); //DeepCopy your image to result
rImage->Update();
double val;
int i = 0;
for(vtkIdType f = result->GetNumberOfPoints()-1; f > -1; f--)
{
val = YourImage->GetPointData()->GetScalars()->GetTuple1(f);
result->GetPointData()->GetScalars->SetTuple1(i,val);
i++;
}
result->Update();
//Now Visualize your image

How to display a binary image in pictureBox VC++

i'm working on a VC++ and OpenCV application, i'm loading images into picturBox and make some OpenCV operations on them, i assign the loaded image into IplImage to make processing on it but then assign the processed image again into the picture box, i write this code to load the image selected from openFileDialog into IplImage ,binarize the image then reassign the binarized image back to the pictureBox
code:
const char* fileName = (const char*)(void*)
Marshal::StringToHGlobalAnsi(openFileDialog1->FileName);
IplImage *img=cvLoadImage(fileName,CV_LOAD_IMAGE_COLOR);
int width=img->width;
int height=img->height;
IplImage *grayScaledImage=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
cvCvtColor(img,grayScaledImage,CV_RGB2GRAY);
cvThreshold(grayScaledImage,grayScaledImage,128,256,CV_THRESH_BINARY);
this->pictureBox1->Image=(gcnew
System::Drawing::Bitmap(grayScaledImage->width,grayScaledImage->height,grayScaledImage->widthStep,
System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr)grayScaledImage->imageData));
but i doesn't find a format which displays a binary image, any help about that.
Original Image:
Converted image:
You seem to be creating an RGB image (System::Drawing::Imaging::PixelFormat::Format24bppRgb) but copying into it a grayscale, presumably the System::Drawing::Imaging function doesn't do conversion - or isn't doing it properly.
Edit: Some more explanation.
Your greyscale image is stored in memory as one byte for each pixel Y0, Y1, Y2,Y3...... Y639 (we use Y for brightness, and assuming a 640 pixel wide image).
You have told the .net image class that this is Format24bppRgb which would be stored as one red,one green and blue byte per pixel (3bytes = 24bpp). So the class takes your image data and assumes that Y0,Y1,Y2 are the red,green,blue values for he first pixel, Y3,Y4,Y5 for the next and so on.
This is using up 3x as many bytes as your image has, so after 1/3 of the row it starts reading the next row and so on - which gives you the three repeated pictures.
ps. the fact that you have turned it into a binary image just means that the Y values are either 0 or 255 - it doesn't change the data size or shape.

OpenCV Image Display Failure

I'm trying to include OpenCV (version 2.3.1) in a project I'm working on. A camera is sending my program (in Microsoft Visual C++ 2008 on a Windows 7 64-bit machine) an image stream, which the program stores in an unsigned 8-bit integer buffer. I would like to display this stream in a window using OpenCV. Right now, I can't seem to get any images to display in my OpenCV windows, so I'm not using my image stream yet; just a JPEG file.
First I declare my window:
namedWindow( "Window", CV_WINDOW_AUTOSIZE );
Then I try to fill it:
char* imgName = "C:\...\Jellyfish.jpg";
Mat imgMat = imread(imgName, 1);
if(imgMat.data)
{
imshow( "Window", imgMat );
}
When my program gets to the point where the window gets declared, a tiny gray window appears. When it reaches the point where it is supposed to display the image, the window's dimensions change to that of the image (I've tested this with different images) but the inside of the window remains a plain gray box.
What is causing this strange error? The program obviously found the image, or it would not have been able to change its dimensions correctly.
You need to add waitKey(2) function call after the imshow.
From OpenCV documentation for waitKey:
This function is the only method in HighGUI that can fetch and handle
events, so it needs to be called periodically for normal event
processing unless HighGUI is used within an environment that takes
care of event processing.
Without this function Windows is unable to handle PAINT event and redraw your window.

Resources