binary matrix to image in octave - matrix

I have a 2D binary matrix and I would like to get a grayscale image from it. Someone suggested using imwrite but the problem is that instead of black and white the colors I get are black and red. The matrix contains only zeros and ones. Any idea why this is happening or how I could get the result I want. I'm running it on OS X. This is the line where I try to create the image. Thank you.
imwrite(matrix, "image.bmp");

You have to convert your matrix to a boolean matrix.
img = logical (matrix);
imwrite (img, "image.bmp");
and for the future: A Code snippet which create some sample image would be better. See https://stackoverflow.com/help/mcve

Related

Distance transform of grayscale image

Matlab has bwdist and graydist: I was looking for something like bwdist for grayscale images, but I wasn't sure what the mask specified in graydist was.
When I'm working with the graydist function, I have tried:
T = graydist(image,image<=0)
T = graydist(image,image<=254)
I can't find an example of it being used other than that one Matlab page, but they're using different input arguments. An explanation or suggestions as to how to get the distance transform of a greyscale image in Matlab would be really helpful!

Extracting pixel values using Pillow - every coordinate I use gives the same value

I am attempting to use the Pillow library in Python 2.7 to extract pixel values at given coordinates on PNG and JPG images. I don't get an errors but I always get the same value irrespective of the coordinates I have used on an image where the values do vary.
This is an extract from my script where I print all the values (its a small test image):
from PIL import Image
box = Image.open("col_grad.jpg")
pixels = list(box.getdata())
print(pixels)
And from when I try to extract a single value:
from PIL import Image, ImageFilter
box = Image.open("col_grad.jpg")
value = box.load()
print(value[10,10])
I have been using these previous questions on this topic for guidance including:
Getting list of pixel values from PIL
How can I read the RGB value of a given pixel in Python?
Thanks for any help with this,
Alex
I'm not sure if you can access it the way you want, because of the complexity of images data.
Just get the pixel:
Image.getpixel(xy)
Returns the pixel value at a given position.
Parameters: xy – The coordinate, given as (x, y).
Returns: The pixel value. If the image is a multi-layer image, this method returns a tuple.:
You should consider doing one of the following:
FIRST convert your image to grayscale and then find the pixel values present.
img_grey= img.convert('1') # convert image to black and white
SECOND If you want the pixel values of RGB channels, you have to split your color image. Then find the pixel values in all the channels at a particular coordinate.
Image.split(img)

Reading data from colour terrain map

I have a question about converting a height-map that is in colour into a matrix - look here to see examples of such maps. If I were to have a terrain plot and plot it using imagesc, then I would see it as a colour map. I was wondering how I could convert an image that looks like this into its corresponding matrix.
This seems like it should be a pretty basic procedure, but I can neither work out how to do it myself nor find out how to do it online (including looking on SO).
To put it another way, the image in question is a jpeg; what I'd like is to be able to convert the .jpg file into a matrix, M say, so that imagesc(M), or surf(M), with the camera looking at the (x,y)-plane (from above), give the same as viewing the image, eg imshow(imread('Picture.jpg')).
You can use Matlab's rbg2ind function for this. All you need to choose is the "resolution" of the output colormap that you want, i.e. the second parameter n. So if you specify n as 8 for example, then your colormap will only have 8 values and your output indexed image should only have 8 values as well.
Depending on the color coding scheme used, you might try first converting the RGB values to HSL or HSV and using the hue values for the terrain heights.

What's the easiest way to extract contour of segmented image?

Let's say i have an image like that one:
After some quick messing around, i got a binary image of the axe, like that:
What is the easiest/fastest way to get the contour of that image using GNU/Octave?
In Octave you can use bwboundaries (but I will welcome patches that implement bwtraceboundaries)
octave:1> pkg load image;
octave:2> bw = logical (imread ("http://i.stack.imgur.com/BoQPe.jpg"));
octave:3> boundaries = bwboundaries (bw);
octave:4> boundaries = cell2mat (boundaries);
octave:5> imshow (bw);
octave:6> hold on
octave:7> plot (boundaries(:,2), boundaries(:,1), '.g');
There are a couple of differences here from #Benoit_11 answer:
here we get the boundaries for all the objects in the image. bwboundaries will also accept coordinates as input argument to pick only a single object but I believe that work should be done by further processing your mask (may be due to the jpeg artifacts)
because we get boundaries for all objects, so you get a cell array with the coordinates. This is why we are using dots to plot the boundaries (the default is lines and it will be all over the image as it jumps from one object to other). Also, it is not documented whether the coordinates given are for the continuous boundary, so you should not assume it (again, why we plot dots).
the image that is read seems to have some artifacts, I will guess that is from saving in jpeg.
You can use bwtraceboundary in the Image package. Here is the Matlab implementation but that should be pretty similar using Octave:
First estimate starting pixel to look for boundary and then plot (BW is the image). (Check here )
dim = size(BW);
col = round(dim(2)/2)-90;
row = min(find(BW(:,col)));
boundary = bwtraceboundary(BW,[row, col],'N');
imshow(BW)
hold on;
plot(boundary(:,2),boundary(:,1),'g','LineWidth',3);
Output:

Matlab rgb2hsv changes image

I am trying to convert an array of RGB values into an array of HSV values in Matlab. I am running the following code:
pic = imread('ColoradoMountains.jpg');
pic = rgb2hsv(pic);
imwrite(pic,'pic.jpg')
But the image that gets written has completely different colors. I've been trying to set the color map to hsv, but I'm not sure if that even makes sense. Nothing on the internet comes up with my keywords, can someone please point me in the right direction?
You can specify the colormap that imwrite is supposed to use. Try this:
imwrite(pic,colormap('HSV'),'pic.png');
Here's the documentation for imwrite: http://www.mathworks.com/help/matlab/ref/imwrite.html
In Matlab you have to distinguish between an indexed image and an 3-channel image. An indexed image is a n*m*1 image where each value of the [0,1] range is associated to a colour. This association is called colour map, which could be for example a unit circle in HSV or RGB. This can be written using imwrite with the map parameter, but this is not what you want.
What you obviously want is an HSV-encoded image, which means the three rgb-channels are converted to three hsv channels. This is (as far as I know) not possible. If you take a look into the documentation of imwrite, you can see how CMYK-Encoded images are written, this is done implicit passing a n*m*4 image. Is there any of the standard file formats which supports HSV? If so I'll take a closer look at this format.

Resources