Octave, display image received by socket connection does't show - image

I have an octave script in which i open a socket server an receive some commands from connected clients. This already works. Now i need to send data to Octave, mostly images and process them. To test this i wanted to receive and display a grayscale test image.
bufflen = 4096;
[data,count]=recv(b,bufflen);
imshow (data)
the image window opens but it is empty. The size of data is exactly the size of the image file i am sending. I also tried saving the image with
imwrite (data, "test.jpg");
this produced a file but every line of the image was in one long line. When i open the image with
imshow test.jpg
everything works as it should, the image window appears and shows the image.
I am sending the data via netcat with
>ncat.exe 127.0.0.1 12346 < test.jpg
this seems to work, i was able to transfer the image to another pc and view it there.
Every hint or tip is greatly appreciated, thank you.

You are sending your jpeg as byte stream (ncat.exe 127.0.0.1 12346 < test.jpg) but you are trying to show is with imshow as it would be an uncompressed RGB, grayscale or index image. You can receive it and save it to a tempfile and then load it with imread. In this case graphics/image-magick will do the uncompression from JPE to RGB to you.

Guessing here since you didn't provide much information, but it sounds like you're data is coming as a vector and you need to reshape it into an array for imshow
>> newdata = reshape(data, 64, 64)
You haven't shown us an example of the input data, so it is also possible that your data is a string of characters, while image arrays need to be numerical values. To verify before reshaping you could run:
>> class(data)
If so you will need to convert it to an array of numerical values. You can use str2num for that, but exactly how to do so will depend on what the string looks like, are there value separators, etc.
See:
https://www.gnu.org/software/octave/doc/interpreter/String-Conversions.html

Related

find cropped area of image inside the basic image with xxd

I done the following:
I made a screenshot of my linux desktop with gnome-screenshot then converted it to a bmp image and dumped it as raw hex values with xxd -ps.[resolution:969x1920]
From the bmp image i made before, i cropped a small area and exported it to another bmp image and dumped it as raw hex values as well with same method as before.[resolution:47x79]
Now when i go and copy a row of hex values (lets say from the end of the file just to avoid any headers) from the smaller image and try to find it on the other dumped file, it shows that there is not there.
I don't know much from image formats, i just want to know if there is something fundamental behind it that i am missing and i have to study before trying again something similar.
Thank you in advance!

Convert image to Blob

I want to upload image data to a php script on the server. I have a URL for an image source (PNG, the image might be located on a different server). I load this into a Javascript image, draw this into a canvas and use the canvas.toBlob() method (or a polyfill as it is not mainly supported yet) to generate a blob holding the image data. This works fine, but I recognized that the resulting blob size is much bigger than the original image data.
In contrast if I use a HTML File input and let the user select an image on the client the resulting blob has equal size to the original image. Can I get image data from a canvas that is equal to the original image size?
I guess the reason is that I loose the PNG (or any image compression) when using the canvas.toBlob() polyfill:
value: function (callback, type, quality) {
var binStr = atob(this.toDataURL(type, quality).split(',')[1]),
len = binStr.length,
arr = new Uint8Array(len);
for (var i=0; i<len; i++ ) {
arr[i] = binStr.charCodeAt(i);
}
callback(new Blob([arr], {type: type || 'image/png'}));
}
I am confused by so many conversion steps via image, canvas, blob - so maybe there is an alternative to get the image data from a given URL and finally append it to FormData to send it to the server?
The method toDataURL when using the png format only uses a limited set of the possible formats available for PNG files. It is the 8bit per channel RGBA (32 bits) compressed format. There are no options to use any of the other formats available so you are forced to include redundant data when you save as a PNG. PNG also has a 24bit and 8 bit format. PNG also has several compression options available though I am unsure which is used but each browser.
In most cases it is best to send the original image. If you need to modify the image and do not use the alpha channel (no transparency) but still want the quality to be high send it as a jpeg with quality set to 1 (max).
You may also consider the use of a custom encoder for PNG that gives you access to more of the PNG encoding options, or even try one of the many other formats available, or make up your own format, though you will be hard pushed to improve on jpeg and webp.
You could also consider compressing the data on the server when you store it, even jpeg and webp have a little room for more compression. For transport you should not worry as most data these days is compressed as it leaves the page and most definitely compressed by the time it leaves the clients ISP

Read a single image to Matlab from a Tiffstack without loading the entire stack

I need to read images from a large stack of Tiff files while the stack is being updated with additional images. I don't wanna load the stack multiple times for performance issues. Is there a trick do read a single image at a time in Matlab?
You can use the Tiff class in Matlab to manipulate a multi-tiff file, i.e. a single tiff file containing many tiff images. Please note that I'm no expert in this regard, but when I first encountered this I was completely lost and after some fooling around with it I could get it to work so I hope it will help you. This should hopefully get you started; and you can easily put this in a loop to select multiple frames form your stack.
In the following code, TiffName refers to the name of your multi-tiff file.
%// Set up Tiff object in 'read' mode
Stack_TiffObject = Tiff(TiffName,'r');
%// Frame you wish to read
FrameToRead = 4;
%// Use setDirectory method to access the image in the stack
Stack_TiffObject.setDirectory(FrameToRead)
%// Read image data
YourImage = Stack_TiffObject.read();
%// Close the Tiff object when you're done
Stack_TiffObject().close
Unless I missed something completely obvious that should load only selected frames from your stack.

Save uint16 tiff image as truecolor with Matlab

I am processing microscopy images (in Matlab) in the tiff format, normally uint8 or uint16. Basically I read them, put them in a cell array for processing and then export them in the tiff format either as an image sequence or a stack (using imwrite and either the 'overwrite' or 'append' writemode property of imwrite, respectively). Up to now everything works very well.
The problem I'm having is the following:
When I open the images with ImageJ, they are not in truecolor "RGB" color mode, but rather in composite mode. For example ImageJ reads the data as 8 bit, which it is, but does not open the image as a truecolor (Sorry for the bad choice of words I don't know the right terminology). Hence I have to manually combine the 3 channels together, which is bothersome for large datasets.
Here is a screen shot explaining. On the left is what I would like,i.e. what I obtain if I open the image directly with ImageJ, and on the right is what I currently have after saving images with Matlab and opening them with ImageJ, which I don't want.
The code I'm using to export the image sequence is the following. "FinalSequenceToExport" is the cell array containing the images.
for i = 1:SliceNumber
ExportedName = sprintf('%s%s%d.tiff',fileName,'Z',i);
imwrite(FinalSequenceToExport{i},ExportedName,'tif','WriteMode','overwrite','Compression','none');
end
If I ask Matlab the size of FinalSequenceToExport{1}, for instance, it gives 512 x 512 x 3.
If I open a given image in the command window and then save it with the same code as above, it does what I want and the resulting image opens as I want in ImageJ. Hence my guess would be that the problem arises from the use of the cell array but I don't understand how.
I hope I've been clear enough. If not please ask for more details.
Thanks for the help!
You need to specify the the 'ColorSpace'
Try this
imwrite(FinalSequenceToExport{i},ExportedName,...
'tif','WriteMode','overwrite','Compression','none', ...
'ColorSpace', 'rgb');
After revisiting this question I found the following to work, thanks to the hint from #Ashish:
imwrite(uint8(FinalSequenceToExport{i}/255),...);
I just needed to divide by 255 after converting to uint8.

Reading a PGM file in Matlab

I'm trying to run the command:
[face,MAP]=imread('face1.pgm');
but I get an empty array for MAP. I was able to successfully read the file data into face though.
PGM is a grayscale image format. There is no colormap. Were you expecting one? The normal behavior for imread is to return an empty colormap if the file didn't contain one.

Resources