Homework 1
Homework 1
Homework 1
ID : 107618401
Answer:
Solution:
∆X = (λ*80) / (λ – Z)
So, ∆X = 8m.
Then, Z = 9λ
Assuming = 25 mm lens (λ), the front lens will have to located at ± 225 mm from the viewing
screen (Z). With the smallest defect having diameter of 0.8mm, then,
∆X = (λ*0.8) / (λ – Z)
Or, ∆X = (25*0.8) / (25 – 225)
Or, ∆X =20/-200 µm.
Or, ∆X = -100 µm.
Where, the minus (-) sign was ignored because it just inverse position coordinate.
0 3 1 2 1 1(q)
1 2 0 1 2 0
0 1 2 3 1 2
1(p) 1 0 1 2 1
Length of 8- = 5
0 3 1 2 1 1(q)
1 2 0 1 2 0
0 1 2 3 1 2
1(p) 1 0 1 2 1
Length of m- = 6
0 3 1 2 1 1(q)
1 2 0 1 2 0
0 1 2 3 1 2
1(p) 1 0 1 2 1
V = {1, 2}
Length of 4- = 0 . There is no 4- path from p to q
0 3 1 2 1 1(q)
1 2 0 1 2 0
0 1 2 3 1 2
1(p) 1 0 1 2 1
Length of 8- = 5
0 3 1 2 1 1(q)
1 2 0 1 2 0
0 1 2 3 1 2
1(p) 1 0 1 2 1
Length of m- = 7
0 3 1 2 1 1(q)
1 2 0 1 2 0
0 1 2 3 1 2
1(p) 1 0 1 2 1
Subtraction:
if (err != E_iVision_ERRORS.E_OK)
{
MessageBox.Show(err.ToString(), "Error");
return;
}
if (err2 != E_iVision_ERRORS.E_OK)
{
MessageBox.Show(err.ToString(), "Error");
return;
}
hbitmap3 = iImage.iGetBitmapAddress(GrayImage3);
if (pictureBox3.Image != null)
pictureBox3.Image.Dispose();
pictureBox3.Image = System.Drawing.Image.FromHbitmap(hbitmap);
hbitmap3 = iImage.iGetBitmapAddress(GrayImage3);
pictureBox3.Refresh();
}
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
IntPtr gray = iImage.CreateGrayiImage();
E_iVision_ERRORS failed = E_iVision_ERRORS.E_NULL;
Int32[] Intensitybar = { 1, 2, 4, 8, 16, 32, 64, 128, 256 };
public Form1()
{
InitializeComponent();
}
if (openFileDialog1.ShowDialog() != DialogResult.OK)
{
MessageBox.Show(failed.ToString(), "No File Chosen!");
return failed;
}
source = openFileDialog1.FileName;
failed = iImage.iReadImage(image, source);
if (failed != E_iVision_ERRORS.E_OK)
{
MessageBox.Show(failed.ToString(), "Wrong Type");
return failed;
}
return E_iVision_ERRORS.E_OK;
}
if (failed == E_iVision_ERRORS.E_OK)
{
finalimage(sender, e, gray);
}
}
// Display Picture
IntPtr temp;
unsafe
{
fixed (byte* bufPointer = &tempMatrix[0, 0])
{
failed = iImage.iPointerToiImage(tempgray, (IntPtr)bufPointer,
newwidth, newheight);
if (failed != E_iVision_ERRORS.E_OK)
{
MessageBox.Show(failed.ToString(), "Error");
return;
}
temp = iImage.iGetBitmapAddress(tempgray);
pictureBox1.Image = System.Drawing.Image.FromHbitmap(temp);
pictureBox1.Refresh();
}
}
iImage.DestroyiImage(tempgray);
}
}
}
Result:
Main Code:
rows1 = (int)(gray.rows/zoomFactor);
cols1 = (int)(gray.cols/zoomFactor);
gray1 = Mat::zeros(cvSize(rows1, cols1), gray.type());
#if (METHOD == NEAREST_NEIGHBOR)
float hRatio = (float)(rows0)/rows1;
float wRatio = (float)(cols0)/cols1;
float rr = 0, cc = 0;
for (int i = 0; i < rows1; i++)
for (int j = 0; j < cols1; j++)
{
//gray1.at<uchar>(i, j) = gray.at<uchar>(i*rows0/rows1,
j*cols0/cols1);
rr = i*hRatio;
cc = j*wRatio;
gray1.at<uchar>(i, j) = gray.at<uchar>((int)rr, (int)cc);
}
#elif (METHOD == BILINEAR)
float hRatio = (float)(rows0-1)/rows1;
float wRatio = (float)(cols0-1)/cols1;
float rr = 0, cc = 0;
float rDiff = 0, cDiff = 0;
int A, B, C, D;
float value;
for (int i = 0; i < rows1; i++)
for (int j = 0; j < cols1; j++)
{
rr = (float)i*hRatio;
cc = (float)j*wRatio;
rDiff = rr - (int)rr;
cDiff = cc - (int)cc;
A = gray.at<uchar>((int)rr, (int)cc);
B = gray.at<uchar>((int)rr, (int)cc+1);
C = gray.at<uchar>((int)rr+1, (int)cc);
D = gray.at<uchar>((int)rr+1, (int)cc+1);
value = (int)(A*(1-rDiff)*(1-cDiff) + B*rDiff*(1-cDiff) +
C*(1-rDiff)*cDiff + D*rDiff*cDiff);
gray1.at<uchar>(i, j) = value;
//gray1.at<uchar>(i, j) = (int)(gray.at<uchar>(rDiff,
cDiff)*(1-rDiff)*(1-cDiff) + gray.at<uchar>(rDiff, cDiff+1)*rDiff*(1-cDiff) +
// gray.at<uchar>(rDiff+1, cDiff)*(1-rDiff)*cDiff +
gray.at<uchar>(rDiff+1, cDiff+1)*rDiff*cDiff);
}
#elif (METHOD == BICUBIC)
double p[4][4] = {{1,3,3,4}, {7,2,3,4}, {1,6,3,6}, {2,5,7,2}};
float hRatio = (float)(rows0)/rows1;
float wRatio = (float)(cols0)/cols1;
float rr = 0, cc = 0;
float rDiff = 0, cDiff = 0;
Nearest-Neighbour:
int[] temp = new int[w2*h2] ;
double x_ratio = w1/(double)w2 ;
double y_ratio = h1/(double)h2 ;
double px, py ;
for (int i=0;i<h2;i++) {
for (int j=0;j<w2;j++) {
px = Math.floor(j*x_ratio) ;
py = Math.floor(i*y_ratio) ;
temp[(i*w2)+j] = pixels[(int)((py*w1)+px)] ;
}
}
Biliner-Code:
#elif (METHOD == BILINEAR)
float hRatio = (float)(rows0-1)/rows1;
float wRatio = (float)(cols0-1)/cols1;
float rr = 0, cc = 0;
float rDiff = 0, cDiff = 0;
int A, B, C, D;
float value;
for (int i = 0; i < rows1; i++)
for (int j = 0; j < cols1; j++)
{
rr = (float)i*hRatio;
cc = (float)j*wRatio;
rDiff = rr - (int)rr;
cDiff = cc - (int)cc;
A = gray.at<uchar>((int)rr, (int)cc);
B = gray.at<uchar>((int)rr, (int)cc+1);
C = gray.at<uchar>((int)rr+1, (int)cc);
D = gray.at<uchar>((int)rr+1, (int)cc+1);
value = (int)(A*(1-rDiff)*(1-cDiff) + B*rDiff*(1-cDiff) +
C*(1-rDiff)*cDiff + D*rDiff*cDiff);
gray1.at<uchar>(i, j) = value;
//gray1.at<uchar>(i, j) = (int)(gray.at<uchar>(rDiff,
cDiff)*(1-rDiff)*(1-cDiff) + gray.at<uchar>(rDiff, cDiff+1)*rDiff*(1-cDiff) +
// gray.at<uchar>(rDiff+1, cDiff)*(1-rDiff)*cDiff +
gray.at<uchar>(rDiff+1, cDiff+1)*rDiff*cDiff);
}
Bicubic Code:
double p[4][4] = {{1,3,3,4}, {7,2,3,4}, {1,6,3,6}, {2,5,7,2}};
float hRatio = (float)(rows0)/rows1;
float wRatio = (float)(cols0)/cols1;
float rr = 0, cc = 0;
float rDiff = 0, cDiff = 0;
Result:
Image Processed by Bilinear:
Image Processed by Nearest-Neighbor:
Image Processed by Bicubic:
Conclusion:
1. When do Shrinking then Zooming, the image will be not clear as the image source.
Each interpolation method will give different results.
2. Nearest-Neighbor interpolation gives the fastest result but worse.
3. Bilinear interpolation gives a better result compared to Nearest-Neighbor
4. Bicubic interpolation gives a nearly same result compared to Bilinear. But, if we watch
carefully, we can see that this methods result is a little more clear and smoother than
Bilinear.
Addition:
- Bilinear interpolation is a method to interpolate value from 2 points by their first degree
polynomial.
- Cubic Interpolation is a method to interpolate value from 4 points by their third degree
polynomial.
- Bicubic interpolation is cubic interpolation in two dimensions. I'll only consider the case
where we want to interpolate a two dimensional grid. We can use the cubic interpolation
formula to construct the bicubic interpolation formula. This method need 16 points to
interpolate the value.
If you want to try to run: You would have to use opencv 2.4.9; change the METHOD predefine to test.