Images turning green-blackish when passing them to dataloaders - image

I'm working with fastai, trying to pass some images to a dataloader. The original images are kind of pinkish, but after passing them they appear mostly as green-black (see image in link below):
Original pinkish image (up) and example images (down) after passing them to dataloader, and the code.
The code I've used for the datablock and to show the images is:
example = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=GrandparentSplitter(),
get_y=parent_label,
item_tfms=Resize(128)) #already tried it without item_tfms just in case, still black-green
dls = example.dataloaders(path)
dls.show_batch(nrows=1, ncols=3)
I tried with .tif and .jpeg images, and both show the same problem. The only thing that comes to my mind is that somewhat somewhere is not reading correctly the color format (RGB according to my original files), or maybe transforming it; but I'm not able to figure it out.
Just in case it's important, I'm working in a Jupyter notebook with a MackBook Air M1.
Thanks!
Irene

Related

What is the format "BM6"?

I am working with an old system of palletes. When I export an image, it also creates an pallete with 15/16 colors. But when I try to replicate the same result, the program does not recognize the image which I just created. To analyse what happened, I opened some images with notepad and compared them with my "creation" and I noticed quite some differences.
But the most interesting thing was that at the beginning of my images' code was the type "BM6", while they should be "png" or "bmp". I think that is some thing of codification, but I cannot find anything about it.
If the image file is properly formed, then you can take your .bm6 file and simple change / rename the extension to .bmp. And it should suddenly be an image.
.bm6 can result from creating an image file using a text editor, as seen here.

Matlab create image from array and save without displaying

Apologies if this is a dupe, I've been searching for over an hour but the search terms are all really broad and I just keep getting the same results. Also I'm fairly new to matlab so apologies for any misunderstandings.
Anywho, I have a matlab program which needs to frequently save an image generated from a matrix, but I just can't figure out how to do that without displaying it first. Basically I'm caught in between two functions, image and imwrite, both only do half of what I want.
image is able to take my matrix and create the desired output, but it just displays it to a figure window
imwrite is able to save an image to a file without displaying it, but the image is completely wrong and I can't find any parameters that would fix it.
Other questions I've seen deal with using imread and managing figures and stuff, but I'm just doing (for example)
matrix = rand(20);
colormap(winter);
image(matrix, 'CDataMapping', 'scaled');
or
matrix = rand(20);
imwrite(matrix, winter(256), 'filename.png');
Is there some way to call the image function such that it doesn't display a figure window and then gets saved to a file? Something analogous to calling imshow and then savefig in matplotlib.
Just do this:
matrix = rand(20);
f = figure('visible', 'off');
colormap(winter);
image(matrix, 'CDataMapping', 'scaled');
print(f, '-dpng', 'filename.png');

MATLAB: overwriting images using print function

I'm using the print function in MATLAB to write images of plots, something like that
print(figure(1),'-dpng','-r300',filename);
But apparently the images are not overwritten, and the original images stay. I was using saveas before, which seems to overwrite the images, but print gives me more output options. Any ideas?
UPDATE: I ended up deleting the files before the printing with a different function.
You can use this:
im = frame2im(getframe(gcf,rec)); %Grabs image of plot as an image
imsave(im, filename); %save image
That syntax may not be 100%, its a while since I've used it.
Also be aware that this isn't perfect - I remember having issues with it grabbing a grey border around the edge of the plot. Also, I think the image may be based on a matlab screenshot.... just something to be aware of
Saving figures in matlab is rather troublesome, especially if the saved image should look like the original figure.
For myself i found the solution in using export_fig.
It's one of the most downloaded fileexchange files - maybe you should give it a try:
http://www.mathworks.de/matlabcentral/fileexchange/23629-export-fig
A small introduction to export_fig can be found at:
https://github.com/ojwoodford/export_fig/blob/master/README.md

how we save multiple images by using imwrite

can anyone help me to save my resulted images by using imwrite
source = 'C:\Y\';
im_number=5;
for i=1:5
image{i}=im2double(imread([source,'Carbon_', num2str(i)],'tif'));
image{i}=double(image{i});
B{i}= Sftfun(image{i});
B{i}=uint32(B{i});
imwrite(B{i},[source,'face_', num2str(i)],'tif');
end
The problem with your code is that you are casting your image to uint32. If you are trying to save your image as a TIF file, you can only save it as 8-bit or 16-bit. Consulting the MATLAB documentation, you can only save with these two bit depths. 32-bit depths are not supported.
Consult the MATLAB documentation for more details: http://www.mathworks.com/help/matlab/ref/imwrite.html
As such, either cast the image as 8-bit or 16-bit (through im2uint8 or im2uint16), or normalize your image so that it goes from [0,1] (through im2double).
I also have some comments about your code that do need fixing for readability:
Do not save your images to a cell array called image. MATLAB has a built-in command called image which takes in a matrix and displays it to the screen for you as an image. Bear in mind this is not the same as imshow. By doing this assignment, you will shadow over the actual image command, and so any scripts that rely on this function will result in an error.
im_number seems to be an unused variable. I'm not sure what its purpose is, but I'd say it's safe to remove this statement as well.
Get rid of the following statement as you are already converting the image to a double type in the previous line:
image{i} = double(image{i});
Aside
It seems that you have asked a similar question here: save tif 32 bit images by using imwrite
This question has already been resolved in that you are not able to save 32-bit images using imwrite. However, someone in this thread has posted a workaround for you to use in MATLAB. Try using that instead of imwrite.

OSX: automated (every 1-2sec) screenshot (not full screen but (x,y,w,h)) using python

I want to make screenshots on OSX using python. I dont want make full screen shots but only certain rectangles on the screen. Something like (291,305,213,31). I need the correct pixel because afterwards the image files are processed by OCR (python-tesseract) to extract the text.
By the way this is since 6 years the first time I am programming, so far I only know Java a bit. I started yesterday and gave up this morning at 4am. So basically I have no clue yet...For example I still cannot build with Sublime because of path settings, but thats a different story. Cant figure out everything on one day.
I was trying already the following:
- wxPython
But the result are black images, see also:
stackoverflow.com/questions/8644908/take-screenshot-in-python-cross-platform
Additionally it only works in 32-bit mode, but when I do OCR using python-tesseract openCV requires 64-bit....
autopy
when trying to install I got errors, see also:
stackoverflow.com/questions/12993126/errors-while-installing-python-autopy
ImageGrab
only Windows
effbot.org/imagingbook/imagegrab.htm
commandline screencapture
os.system('screencapture test.png')
When I found this I thought, nice but only fullscreen when checking man screencapture. But then I found this: guides.macrumors.c om/screencapture
-R capture screen rect
That would be already enough, but on OSX 10.7.5 I dont have this option. Any ideas?
import Quartz.CoreGraphics
neverfear.org/blog/view/156/OS_X_Screen_capture_from_Python_PyObjC
Create screenshot as CGImage
image = CG.CGWindowListCreateImage(
region,
CG.kCGWindowListOptionOnScreenOnly,
CG.kCGNullWindowID,
CG.kCGWindowImageDefault)
Unfortunately the image is not in file format but a CGImage, no idea how to save as file.
So if possible I would like to use the commandline screencapture with -R if somebody knows how. Just as a start to continue.
Are there any other command line tools available?
What about other libs that I have missed?
Cheers
M
Given that you can get a CGImageRef, you can get its pixel data using the techniques described in Technical Q&A QA1509: Getting the pixel data from a CGImage object. In particular, it shows a function to get the pixel data as a CFDataRef using this function:
CFDataRef CopyImagePixels(CGImageRef inImage) { return CGDataProviderCopyData(CGImageGetDataProvider(inImage)); }
and says:
The pixel data returned by CGDataProviderCopyData has not been color
matched and is in the format that the image is in, as described by the
various CGImageGet functions …
It shows an alternative for getting the pixel data in other formats if you need that.

Resources