Lab 7
1 Calculating Index
B _ c o l u m n _ i n d e x = A _ c o l u m n _ i n d e x + r e g i o n _ w i d t h − 1 = 5 + 8 − 1 = 12 \begin {aligned}B\_column\_index&=A\_column\_index + region\_width - 1\\&=5+8-1\\&=12\end {aligned} B_column_index=A_column_index+region_width−1=5+8−1=12
So the index of pixel (B) would be column index 12.
Since A and B are on the same row, they should have the same row index.
Therefore, the index of the upper right pixel (B) is img[3][12]
2 Mirror/Flip an Image
2.1
-
even case
M _ i n d e x = A _ i n d e x + ( i m a g e _ w i d t h / 2 ) − 1 = 5 + ( 8 / 2 ) − 1 = 8 \begin {aligned}M\_index&=A\_index+(image\_width / 2) - 1\\&=5+(8 / 2) - 1\\&=8\end {aligned} M_index=A_index+(image_width/2)−1=5+(8/2)−1=8
N _ i n d e x = A _ i n d e x + i m a g e _ w i d t h / 2 = 5 + 8 / 2 = 9 \begin {aligned}N\_index&=A\_index+image\_width / 2\\&=5+8 / 2\\&=9\end {aligned} N_index=A_index+image_width/2=5+8/2=9
-
odd case
M _ i n d e x = A _ i n d e x + ( i m a g e _ w i d t h − 1 ) / 2 = 5 + ( 7 − 1 ) / 2 = 8 \begin {aligned}M\_index&=A\_index+(image\_width - 1) / 2\\&=5+(7 - 1) / 2\\&=8\end {aligned} M_index=A_index+(image_width−1)/2=5+(7−1)/2=8
2.2
If the region width is denoted as w
, and we start the loop at pixel A (img[i][j]
), the index of the last pixel can be calculated as follows:
L a s t _ p i x e l _ i n d e x = A _ c o l u m n _ i n d e x + ( i m a g e _ w i d t h / / 2 − 1 ) = j + w / / 2 − 1 \begin {aligned}Last\_pixel\_index&=A\_column\_index+(image\_width // 2-1)\\&= j + w // 2-1\end {aligned} Last_pixel_index=A_column_index+(image_width//2−1)=j+w//2−1
Take above two cases as examples,
- In the even case: w = 8 w=8 w=8, the for loop should end at the index 5 + 8 / / 2 − 1 = 8 5+8//2-1=8 5+8//2−1=8
- In the odd case: w = 7 w=7 w=7, the for loop should end at the index 5 + 7 / / 2 − 1 = 7 5+7//2-1=7 5+7//2−1=7
Therefore, the for loop should end at the index j + (w // 2) - 1
.
3 Cropping an image in half horizontally
code
from FudanImgLib import *
def crop_top_half(img):
# TODO: calculate new height new_height =
new_height = len(img) // 2
# TODO: create the new image to store the cropped version cropped_image =
cropped_image = create_img(new_height, width(img), (0, 0, 0))
# TODO: loop through the new image and set each pixel
for i in range(new_height):
for j in range(width(img)):
cropped_image[i][j] = img[i][j]
return cropped_image
test cases
from FudanImgLib import *
img_even_height = [[(255, 0, 0), (255, 99, 33)], [(66, 120, 240), (198, 0, 240)],
[(16, 20, 38), (98, 170, 5)], [(16, 20, 38), (98, 170, 5)],
[(3, 0, 13), (5, 0, 0)], [(55, 0, 0), (255, 99, 33)]]
img_odd_height = [[(16, 20, 38), (98, 170, 5)], [(66, 120, 240), (198, 0, 240)],
[(255, 0, 0), (255, 99, 33)],[(3, 3, 3), (25, 9, 133)],
[(3, 0, 13), (5, 0, 0)]]
print(crop_top_half(img_even_height))
print(crop_top_half(img_odd_height))
img = load_img('Shanghai.png')
cropped_img=crop_top_half(img)
save_img(cropped_img, 'cropped.png')
result
running result:
- every result matches the expectation