Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Calibrating Optics: Is square still a square?

Subject: Calibrating Optics: Is square still a square?

From: Prakhar

Date: 28 Mar, 2012 22:08:12

Message: 1 of 6

Hello Everyone

This might be a redundant questions, however i am still asking it because i am sort of stuck here. I am trying to calibrate the optics. For this, i have taken an image of a grid (made out of plexiglass) which has 1/8" square blocks machined on it. Now, i want to make sure that in the image square is still a square i.e. pixel distance in x is same as that in y. The image is: 840x1728x3 in .bmp format.

This is what i have written so far:

A = imread('Calib_001.bmp');
A = double(A(:,:,1));
% Bicubic Interpolation
B = imresize(A, 1.0, 'bicubic');
% selecting a square region
C = B(500:510, 250:260);
w = size(C,1);
h = size(C,2);

How should i proceed next? Do i just calculate distance between two pixels? Or do i have to identify the boundaries of the square using pixel intensity at that point? Any help will be appreciated.

Thanks
Prakhar

Subject: Calibrating Optics: Is square still a square?

From: Steven_Lord

Date: 29 Mar, 2012 13:34:30

Message: 2 of 6



"Prakhar " <prakhar_cool@yahoo.com> wrote in message
news:jl024c$d6m$1@newscl01ah.mathworks.com...
> Hello Everyone
>
> This might be a redundant questions, however i am still asking it because
> i am sort of stuck here. I am trying to calibrate the optics. For this, i
> have taken an image of a grid (made out of plexiglass) which has 1/8"
> square blocks machined on it. Now, i want to make sure that in the image
> square is still a square i.e. pixel distance in x is same as that in y.
> The image is: 840x1728x3 in .bmp format.
>
> This is what i have written so far:
> A = imread('Calib_001.bmp');
> A = double(A(:,:,1));
> % Bicubic Interpolation
> B = imresize(A, 1.0, 'bicubic');
> % selecting a square region
> C = B(500:510, 250:260);
> w = size(C,1);
> h = size(C,2);
>
> How should i proceed next? Do i just calculate distance between two
> pixels? Or do i have to identify the boundaries of the square using pixel
> intensity at that point? Any help will be appreciated.

If you're displaying this with IMAGE, IMAGESC, IMSHOW, or something similar
I think all you'll need is "axis equal."

http://www.mathworks.com/help/techdoc/ref/axis.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Calibrating Optics: Is square still a square?

From: ImageAnalyst

Date: 29 Mar, 2012 00:35:45

Message: 3 of 6

One simple, but manual way is to use ginput() to have the user locate
corners of what should be a perfect square. Then analyze the (x,y)
coordinates of the corners to get the two lengths in the x direction
and then average them, and then do the same for the y direction . So
then you have one calibration factor for the x direction and another
for the y direction. Would that work for you? There are more
sophisticated methods but why don't you start with that?

Subject: Calibrating Optics: Is square still a square?

From: ImageAnalyst

Date: 29 Mar, 2012 16:35:25

Message: 4 of 6

On Mar 29, 9:34 am, "Steven_Lord" <sl...@mathworks.com> wrote:
> If you're displaying this with IMAGE, IMAGESC, IMSHOW, or something similar
> I think all you'll need is "axis equal."
--------------------------------------------------------------
That won't help with non-square pixels, for example if your image was
captured by a line scan camera (say aboard an aircraft or spacecraft)
http://en.wikipedia.org/wiki/Pixel_aspect_ratio

Subject: Calibrating Optics: Is square still a square?

From: Prakhar

Date: 2 Apr, 2012 21:01:13

Message: 5 of 6

Thanks a lot for the reply. I tried using imtool and found that distance in x and y are not same. However, i need a more better correction. Here is the image which i have:

http://s13.postimage.org/rjsfxoc93/Calib_002.png

I read on this forum about 'edge' function..so i tried the following:
edge(I, 'sobel')

This found the edges for me. However, i would like to measure there length and after that i have to correct the image for distortion. Am i on right track?

Thanks!!
Prakhar


ImageAnalyst <imageanalyst@mailinator.com> wrote in message <d8c03d0a-1f76-4a46-a351-2e9d0a3eeeec@z17g2000yqf.googlegroups.com>...
> One simple, but manual way is to use ginput() to have the user locate
> corners of what should be a perfect square. Then analyze the (x,y)
> coordinates of the corners to get the two lengths in the x direction
> and then average them, and then do the same for the y direction . So
> then you have one calibration factor for the x direction and another
> for the y direction. Would that work for you? There are more
> sophisticated methods but why don't you start with that?

Subject: Calibrating Optics: Is square still a square?

From: ImageAnalyst

Date: 2 Apr, 2012 21:39:00

Message: 6 of 6

Like I said in your duplicate post, I'd try the hough transform. You
might mask off the non-grid stuff first. Here's code for masking your
image:

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Get the full filename, with path prepended.
folder = 'C:\Users\yourname\Documents\Temporary';
baseFileName = 'Calib_002.png.jpg';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 3, 1);
imshow(rgbImage, []);
axis 'on';
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

% Get the red channel
grayImage = rgbImage(:,:,1);
subplot(2, 3, 2);
imshow(grayImage);
title('Red Channel', 'FontSize', fontSize);

% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 3, 3);
bar(pixelCount);
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
grid on;

binaryImage = grayImage < 45;
subplot(2, 3, 4);
imshow(binaryImage);
title('Red Channel', 'FontSize', fontSize);

% Seal off the holes, fill them, and get rid of non-grid stuff.
binaryImage(end, :) = true;
binaryImage(250:end, 1) = true;
binaryImage(250:end, end) = true;
binaryImage = imfill(binaryImage, 'holes');
% Get rid of small specks.
binaryImage = bwareaopen(binaryImage, 5000);
binaryImage = logical(binaryImage - imclearborder(binaryImage));
subplot(2, 3, 4);
imshow(binaryImage);
title('Binary Mask', 'FontSize', fontSize);
% Mask the original image
maskedImage = grayImage;
maskedImage(binaryImage) = 0;
subplot(2, 3, 5);
imshow(maskedImage);
title('Masked Image', 'FontSize', fontSize);

% % Extract edges.
% BW = edge(maskedImage,'canny');
% imshow(BW);
% title('Edge Image', 'FontSize', fontSize);
% % Do the hough transform:
% [H,T,R] = hough(BW,'RhoResolution',0.5,'Theta',-90:0.5:89.5);
% subplot(2, 3, 6);

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us