MATLAB: Extract point array from .tif image - image

I am relatively new to Matlab image processing stuff. I will be precise:
I have a .tif image which I would like to load into Matlab and subsequently extract an array of points (x,y) by simple mouse clicking. For Example:
I click three times => 3 xy coordinates => 2 dim array in workspace with three rows.
Any ideas?
Thanks.

You should look into imread for loading tif image from disk.
You will find imshow useful for displaying the image you read from disk.
Finally, you can use ginput to interactively mark locations on the displayed image.

Related

How to find images with a variable sized grey rectangle (JPEG corruption) in them?

I had to recover a hard drive and a lot of photos in it came out corrupted. I'm talking about 200.000 photos. I already wrote a script that finds corrupted JPEGs. But some of these images are not corrupted on a file format level. Yet they appear as the example I am showing. The grey part i suspect is data missing from the file. The grey part size is variable and sometimes it has an incomplete line in it.
So I'm thinking I could write or find a script that finds grey rectangles in these images.
How do I do this? Something that opens the image data and looks for this giant grey rectangle? I have no idea where to start. I can code in a bunch of languages.
Any help/examples, is much appreciated.
I was thinking that the grey rectangle is always the same colour, so I created a function to see if that grey is one of the top 10 most frequent
colours.
If the colour had changed, then I would have adjusted the code accordingly to check if the top colour is at least 10x more frequent than the second most frequent colour.
Didn't have to learn feature detection this time. Shame. :(
from collections import Counter
from PIL import Image
# Open the image file
image = Image.open(file)
# Convert the image to RGB format (if it's not already)
image = image.convert('RGB')
# Get a list of all the pixels in the image
pixels = list(image.getdata())
# Count the number of pixels with each RGB value
counts = Counter(pixels)
most_common_colors = counts.most_common(10)
return (128,128,128) in [t[0] for t in most_common_colors]

Matlab subplot shows tiny images

I have an algorithm that does a set of 8 image processing operations in an input image and then I want to show the output of each of them in a grid of 8 images. The problem is that I want to show image by image after each of the operations ends. By using subplot and imshow the image outputs I want to show in the grid are small.
Here is some piece of my code
output1=image_operation(input_image);
subplot(4,4,1);
imshow(output1);
I have heard about imdisp and montage functions, but they don't do what I want. I want to show the first image when the first algorithm ends, then the second image together with the first when the second algorithm ends and so on. What these functions do is showing all images at once and I don't want this.
Is there anything I missed?
The reason why your output images are too small is that you want to plot 8 images but you're using subplot command for 16 images i.e. subplot(4,4,x). Since you want to plot 8 images using subplot, use any of following instead:
subplot(4,2,x)
or subplot(2,4,x)

How to identify and display images from a MATLAB .mat data file?

I have a MATLAB file (xyz.mat), and apparently there are image data in this file but I have very little experience with MATLAB and have no clue how to 'extract/open' them.
This is the only clue I have:
The Matlab data file contains a structure "data" with a field "dataList" which is itself a structure array with one element per image. So the first image can be found in data.dataList(1).img
After loading the file into MATLAB (nothing happened) and typing the command data.dataList(1).img (I got a huge list of numbers) I still get no image.
Any help/ideas?
If data.dataList(1).img are 2D or 3D (check using size), you can use imshow to visualize this 2D array (grayscale), or 3D array (color) as an image.
im = data.dataList(1).img;
figure; imshow(im, []);
You can find the range of this image using min(im(:)) and max(im(:)) or plot the distribution of it's values using imhist.
To view all images as a rectangular montage look into montage function:
montage(I) displays all the frames of a multiframe image array I in a
single image object. I can be a sequence of binary, grayscale, or
truecolor images. A binary or grayscale image sequence must be an
M-by-N-by-1-by-K array.
In effect, you can put a number of K images (of the same M x N size) in an M x N x 1 x K array and invoke montage:
for k = 1:K
I(:,:,1,k) = data.dataList(k).img;
end
figure; montage(I);

Accesing the pixel value dor selected ROA in openCV

I am working on Image Inpaiting on video project and i am selecting the portion of the image on a screen(ROI) as a rectangle and i am inpaiting that portion of the image. Now i want to save five previous frame from live video(that i can do it) and save the pixels value of that ROI of five frame/image on five different array. I will use that array to generate the background and remove the foreground object.
Any one know how i can save pixel value in array for that selected area?
Thanks in advance.
C++ interface of opencv use cv::Mat for storing image pixel.
The following code shows you how to declare a matrix B "pointing" to a ROI of matrix A.
Matrices are images. ROI is a rectangle (x=0;t=0;width=0;height=100). Use opencv highgui function to save your image.
cv::Mat A(640,480,CV_8C3);
cv::Rect rect(0,0,100,100);
cv::Mat B = A(rect);
cv::imwrite("my_roi.pbg",B);
If you need to read frames from a video, use cv::VideoCapture cap and cap >> frame to grab and retrieve each images as cv::Mat. If you want to go to different position in your video file use cv::VideoCapture::set(..,CV_FRAME_MSEC) , read manual.

How do I save a plotted image and maintain the original image size in MATLAB?

I'd like to show an image and plot something on it and then save it as an image with the same size as the original one. My MATLAB code is:
figH = figure('visible','off');
imshow(I);
hold on;
% plot something
saveas(figH,'1','jpg');
close(figH);
But the resulting image "1.jpg" has saved non-image areas in the plot as well as the image. How can I solve this problem?
The reason your new image is bigger than your original is because the SAVEAS function saves the entire figure window, not just the contents of the axes (which is where your image is displayed).
Your question is very similar to another SO question, so I'll first point out the two primary options encompassed by those answers:
Modify the raw image data: Your image data is stored in variable I, so you can directly modify the image pixel values in I then save the modified image data using IMWRITE. The ways you can do this are described in my answer and LiorH's answer. This option will work best for simple modifications of the image (like adding a rectangle, as that question was concerned with).
Modify how the figure is saved: You can also modify how you save the figure so that it better matches the dimensions of your original image. The ways you can do this (using the PRINT and GETFRAME functions instead of SAVEAS) are described in the answers from Azim, jacobko, and SCFrench. This option is what you would want to do if you were overlaying the image with text labels, arrows, or other more involved plot objects.
Using the second option by saving the entire figure can be tricky. Specifically, you can lose image resolution if you were plotting a big image (say 1024-by-1024 pixels) in a small window (say 700-by-700 pixels). You would have to set the figure and axes properties to accommodate. Here's an example solution:
I = imread('peppers.png'); %# Load a sample image
imshow(I); %# Display it
[r,c,d] = size(I); %# Get the image size
set(gca,'Units','normalized','Position',[0 0 1 1]); %# Modify axes size
set(gcf,'Units','pixels','Position',[200 200 c r]); %# Modify figure size
hold on;
plot(100,100,'r*'); %# Plot something over the image
f = getframe(gcf); %# Capture the current window
imwrite(f.cdata,'image2.jpg'); %# Save the frame data
The output image image2.jpg should have a red asterisk on it and should have the same dimensions as the input image.

Resources