Crop the center of an image - python-imaging-library

How can l use the function crop() from PIL to crop the center of an image of 224*224.
Given an input image of :
320*240, crop the center of this image of dimension 224*224
Expected output :
cropped center of the image of dimension 224*224

Starting with this image where the coloured part is 224x224 on a black background of 320x240.
I would just use numpy to crop like this:
#!/usr/local/bin/python3
from PIL import Image
import numpy as np
# Open the image and convert to numpy array
im=Image.open('start.png')
im=np.array(im)
# Work out where top left corner is
y=int((320-224)/2)
x=int((240-224)/2)
# Crop, convert back from numpy to PIL Image and and save
cropped=im[x:x+224,y:y+224]
Image.fromarray(cropped).save('result.png')

Related

classification using 4-channel images in pytorch

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.

Region of interest extraction in MATLAB

I am writing a MATLAB code to implement a specific filter on a selected (from auto ROI) grayscale region of a forearm image which consists of veins. I also uploaded the forearm of a subject (after foreground has extracted).
Basically, I have NIR camera images of the forearm of different subjects with different orientations. I wrote the code that has extracted the foreground grayscale image of the arm, that gave me the white background with the forearm. I used Sobel edge to find edges. I also found the nonzero indices using the find function. I got the row and col indices. I need an idea on how to extract image inside (almost 10 pixels) of the edges detected on both sides of the forearm (black and white edged image-also uploaded).
Sobel-edge:
Foreground image:
ROI image that I need to extract:
clear all
close all
clc
image= rgb2gray(imread('Subj1.jpg'));
image1=~im2bw(image,0.1);
image1=im2uint8(image1);
foreground=imadd(image1,image);
imshow(foreground);
edgesmooth=medfilt2(foreground);
sobeledge= edge(edgesmooth,'sobel');
sobeledge=im2uint8(sobeledge);
figure
imshow(sobeledge);
[col,row]=find(sobeledge~=0);
Starting from the mask image that you make here:
image1=~im2bw(image,0.1);
but inverted, such that the mask is zero for the background and non-zero for the foreground:
image1 = im2bw(image,0.1);
you can use imdilate to expand it by a fixed distance:
se = strel('disk',20); % This will extend by 20/2=10 pixels
image2 = imdilate(image1,se);
image2 will be like image1, but expanded by 10 pixels in all directions.
imerode does the opposite, it shrinks regions.

Matlab import B&W 16-bit tiff then plot?

I have a 16-bit tiff that is b&w. It has no color mapping.
I import it like the following:
Tiff = imread('MyImage.tif')
That gives me a Variable with a value 'single' named Tiff. It is just a grid/matrix of intensity values for each pixel.
I have tried then using
image(Tiff);
But I end up with an image that is all Yellow.
If I do
imagesc(Tiff);
Then it kind of works, but its not grayscale, it is more like a heat map.
How do I plot the tiff on a graph? I want to be able to then then graph other lines on top of that tiff image.
Try this,
[I,cmap] = imread('your_image.tif');
img = ind2rgb(I,cmap);
To plot something on top of your image you can do this:
figure, imshow(img);
hold on;
plot(your_x_data,your_y_data); % or whatever yo want plot on top that image.

Bounding box of an object in an image Matlab

I want to get the object's bounding box positions (x, y, width and height) in the image and save it to a text file. As shown in the below image. So if anyone could please advise.
The image can be found here
Assuming you have read the image using imread, this should work:
bwImage=~im2bw(img,0.98); %making gray pixels white and (almost) white pixels black
bndBox=regionprops(bwImage,'BoundingBox');
bndBox will have top-left corner of the bounding box and its width and height stored in the format [corner_x corner_y width height].

2D binary matrix conversion to an black and white image

I have a 2D binary matrix. How can I convert this into a black and white image?
you should write
I = mat2gray(A)
you can make a
imshow(I)
to see it working.