classification using 4-channel images in pytorch - classification

I have some gray scale and color images with label. I want to combine this gray and color images (4-channel) and run transfer learning using 4-channel images. How to do that?

If I understand the question correctly you want to combine 1 channel images and 3 channel images and get a 4 channel image and use this as your input.
If this is what you want to do you can just use torch.cat().
Some example code of loading two images and combining them along the channel dimension
import numpy as np
import torch
from PIL import Image
image_rgb = Image.open(path_to_rgb_image)
image_rgb_tensor = torch.from_numpy(np.array(image_rgb))
image_rgb.close()
image_grayscale = Image.open(path_to_grayscale_image))
image_grayscale_tensor = troch.from_numpy(np.array(image_grayscale))
image_grayscale.close()
image_input = torch.cat([image_rgb_tensor, image_grayscale_tensor], dim=2)
I assumed that the grayscale image you want to use translated to a tensor with the shape [..., ..., 1] and the rgb image to [..., ..., 3].

your current model expects an RGB input with only three channels, thus its first conv layer has in_channels=3 and the shape of this first layer's weight is out_channelsx3xkernel_heightxkernel_width.
In order to accommodate 4 channel input, you need to change the first layer to have in_channels=4 and a weight of shape out_channelsx4xkernel_heightxkernel_width. You also want to preserve the learned weights, so you should initialize the new weight to be the same as the old except for tiny noise in the added weights.

Related

Scramble the pixels of black and white images in Matlab

I have a series of black and white images (not greyscale, black and white; 2D matrices in Matlab), and I need to randomly scramble the pixels. I found this package in Mathworks File Exchange (https://it.mathworks.com/matlabcentral/fileexchange/66472-image-shuffle); one of the functions, imScrambleRand, does exactly what I need, but it works for RGB images (3D matrices). Is there a way to transform b&w images into 3D matrices so that I can use that function? Or can anyone suggest any other script that does what I need? Keep in mind that I'm not familiar with Matlab, but I'll do my best.
Thank you.
EDIT 1: When I import the BW image I get a 2D matrix of logic values (0 = black, 1 = white). I think the different data format (logic vs integer) is what yields errors when using the function for RGB images.
EDIT 2: I adapted the demo code from the aforementioned package and I used the suggestion by #Jonathan for transforming a 2D matrix into a 3D matrix, and added a loop to transform the logic values into RGB integer values, then use the imScrambleRand function. It works, but what I obtain is the following image: SCRAMBLED IMAGE. This is the BW picture I start with: BW IMAGE. So I checked the scrambled image, and the function from the FEX file actually scrambles within the RGB values, meaning that I found, for instance, a pixel with RGB 0,255,0. So I solved a problem but actually there's a problem within the function: it doesn't scramble pixels, it scrambles values generating colors that were not in the original picture.
EDIT 3: I used the code provided by #nhowe and I obtain exactly what I need, thanks!
EDIT 4: Ok, turns out it's not ok to scramble the pixels since it makes the image too scattered and different from the starting image (you don't say?), but I need to scramble BLOCKS OF PIXELS so that you can't really recognize the image but the black pixels are not too scattered. Is there a way to do that using the code provided by #nhowe?
EDIT 5: It should be ok with this function: https://it.mathworks.com/matlabcentral/fileexchange/56160-hio-been-hb-imagescramble
A simple way to scramble matrix M:
r = rand(size(M));
[~,ri] = sort(r(:));
M(ri) = M;
The simplest solution to go from grayscale to RGB might be this:
rgbImage = cat(3, grayImage, grayImage, grayImage);
Then apply your function from FEX and extract one color channel, assuming that the FEX function will yield three identical color channels.

MATLAB imshow for 3D color image

I have a medical imaging matrix of size [200x200x200].
In order to display it, I am currently using imshow3D function, which is an excellent tool, built by Maysam Shahedi.
This tool displays the 3D image slice by slice, with mouse based slice browsing
In my current project, I generate an RGB image for each z-layer from the original input image. The output is a 3D color image of size [200x200x200x3] (each layer is now represented by 3 channels).
The imshow3D function works great on grayscale images. Is it possible to use it to display RGB images?
I took a look at this nice imshow3D function from Matlab FileExchange, and it is quite straight-forward to change it to allow working with a stack of RGB images.
The magic part of the function is
imshow(Img(:,:,S))
which displays the slice S of the image Img. We can simply change it to show all 3 channels of image S by changing this to Img(:,:,S,:). The result will be of size 200-by-200-by-1-by-3, while MATLAB expects RGB images to be of size 200-by-200-by-3. Simply squeeze this image to get the correct dimension. This results in:
imshow(squeeze(Img(:,:,S,:))
So to show RGB images, do a search-and-replace inside the function imshow3D, to replace all occurrences of Img(:,:,S) with squeeze(Img(:,:,S,:)) and it works!

Create a model of background from multiple images

I am trying to create a model of background from multiple images of the same size. I want to use the model to segment moving objects from the roller belt (background model), but I'm not sure how to achieve this.
My concern is how to compute background model with 4 images representing roller belt?
I was thinking of calculating mean for each belt image, add it together, and divide it by the number of images, but in this case I will end up with one value for the whole background. How can I compute the mean of each pixel by looking at 4 images provided?
%some example data
A{1}=imread('http://dummyimage.com/600x400/00a/fff.jpg&text=a');
A{2}=imread('http://dummyimage.com/600x400/00a/fff.jpg&text=b');
A{3}=imread('http://dummyimage.com/600x400/00a/fff.jpg&text=c');
A{4}=imread('http://dummyimage.com/600x400/00a/fff.jpg&text=d');
%make sure every image is of type double. Every uint type is possible as well, but requires casting at the end.
for ix=1:numel(A),A{ix}=im2double(A{ix});end
%concatinate on ndims+1, which means 4th dimension for color images and third dimension for grey scale images.
I=mean(cat(ndims(A{1})+1,A{:}),ndims(A{1})+1);
imshow(I);
If the images are im1,im2,im3,im4 calculate the mean along the third dimension.
% concatenate the images to a single 3-D stack.
im = cat(3,im1,im2,im3,im4);
% find the mean of each pixel.
meanIm = mean(im,3);

How to get the red image from gray image in matlab

i have an image let say a=imread('example.bmp'i got all three channel from it :
R=a(:,:,1);
G=a(:,:,2);
B=a(:,:,3);
and i have the gray image of it:
igray=rgb2gray(a);
Can I get the red component from the gray image ?
No, you can't, since igray will be a two dimensional image (a is three dimensional, with the third dimension being colors planes), containing only intensity values for each pixel.
To convert an RGB image to grayscale, rbg2gray uses a formula you can find here
As you can see, it's a 3 variables equation, therefore you can't find them using intensity value alone.
The rgb2gray function effectively does this to every RGB pixel (type edit rgb2gray):
Gray = 0.298936021293776*Red+0.587043074451121*Green+0.114020904255103*Blue;
If you only have Gray in the equation above then you have one equation with three unknowns. More information is needed to solve for Red.
If you just want an RGB image where every channel has the same components, i.e., those created by rgb2gray, then use
igray(:,:,3) = rgb2gray(a); % Set last component first to fully allocate array
igray(:,:,1) = igray(:,:,3);
igray(:,:,2) = igray(:,:,3);
Or an RGB image where all the channels are equivalent to the red channel:
igray(:,:,3) = a(:,:,1);
igray(:,:,1) = a(:,:,1);
igray(:,:,2) = a(:,:,1);
The repmat function can be used as well if you prefer.
While nothing in horchler's very long answer is incorrect, I think you just want to get the red channel from the rgb image which is very easy.
A=imread('colorImg.jpg')
redChannel=A(:,:,1)
That's it!
That will return a matrix of type uint8, to convert to double, just do double(redChannel) and you can multipy/divide it by 255 as necessary.

how to detect colour from an image matlab?

we are doing a mat lab based robotics project.which actually sorts objects based on its color so we need an algorithm to detect specific color from the image captured from a camera using mat lab.
it will be a great help if some one can help me with it.its the video of the project
In response to Amro's answer:
The five squares above all have the same Hue value in HSV space. Selecting by Hue is helpful, but you'll want to impose some constraints on Saturation and value as well.
HSV allows you to describe color in a more human-meaningful way, but you still need to look at all three values.
As a starting point, I would use the rgb space and the euclidian norm to detect if a pixel has a given color. Typically, you have 3 values for a pixel: [red green blue]. You also have also 3 values defining a target color: [255 0 0] for red. Compute the euclidian norm between those two vectors, and apply a decision threshold to classify the color of your pixel.
Eventually, you want to get rid of the luminance factor (i.e is it a bright red or a dark red?). You can switch to HSV space and use the same norm on the H value. Or you can use [red/green blue/green] vectors. Before that, apply a low pass filter to the images because divisions (also present in the hsv2rgb transform) tend to increase noise.
You probably want to convert to the HSV colorspace, and detect colors based on the Hue values. MATLAB offers the RGB2HSV function.
Here is an example submission on File Exchange that illustrate color detection based on hue.
For obtaining a single color mask, first of all convert the rgb image gray using rgb2gray. Also extract the desired color plane from the rgb image ,(eg for obtaining red plain give rgb_img(:,:,1)). Subtract the given plane from the gray image........