Dodal se one_a
parent
25a6337328
commit
c5e79f5bac
|
@ -80,6 +80,9 @@ def convolve(I: np.ndarray, *ks):
|
|||
I = cv2.filter2D(I, cv2.CV_64F, k)
|
||||
return I
|
||||
|
||||
def convert_float64_array_to_uint8_array(a: npt.NDArray[np.float64]) -> npt.NDArray[np.uint8]:
|
||||
return a.astype(np.uint8)
|
||||
|
||||
|
||||
if __name__ == '__main__': # False if this file is imported to another file and executed from there.
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ import UZ_utils as uz
|
|||
import numpy as np
|
||||
import numpy.typing as npt
|
||||
from matplotlib import pyplot as plt
|
||||
import random
|
||||
|
||||
#######################################
|
||||
# EXCERCISE 1: Basic image processing #
|
||||
|
@ -11,7 +12,7 @@ def excercise_one() -> None:
|
|||
image = one_a()
|
||||
one_b(image)
|
||||
one_c(image)
|
||||
one_d(100, 200, 50, 200, image) # needs to be fixed
|
||||
one_d(100, 200, 50, 200, image)
|
||||
one_e()
|
||||
|
||||
def one_a() -> npt.NDArray[np.float64]:
|
||||
|
@ -124,8 +125,67 @@ def one_e() -> None:
|
|||
# EXCERCISE 2: Thresholding and histograms #
|
||||
############################################
|
||||
|
||||
def excercise_two() -> None:
|
||||
"""
|
||||
Thresholding an image is an operation that produces a binary image (mask) of the same
|
||||
size where the value of pixels is determined by whether the value of the corresponding
|
||||
pixels in the source image is greater or lower than the given threshold.
|
||||
"""
|
||||
two_a()
|
||||
|
||||
def two_a() -> tuple[npt.NDArray[np.float64], npt.NDArray[np.uint8]]:
|
||||
"""
|
||||
Create a binary mask from a grayscale image. The binary mask is a matrix the same
|
||||
size as the image which contains 1 where some condition holds and 0 everywhere
|
||||
else. In this case the condition is simply the original image intensity. Use the image
|
||||
bird.jpg. Display both the image and the mask.
|
||||
"""
|
||||
random_number = random.random()
|
||||
TRESHOLD = 0.4
|
||||
|
||||
image = uz.imread_gray("./images/bird.jpg")
|
||||
binary_mask = image.copy()
|
||||
|
||||
if random_number < 0.5:
|
||||
binary_mask[binary_mask < TRESHOLD] = 0
|
||||
binary_mask[binary_mask >= TRESHOLD] = 1
|
||||
else:
|
||||
binary_mask = np.where(binary_mask < TRESHOLD, 0, 1)
|
||||
|
||||
binary_mask = uz.convert_float64_array_to_uint8_array(binary_mask)
|
||||
|
||||
fig, (ax0, ax1) = plt.subplots(1, 2)
|
||||
fig.suptitle("Birdie and its mask")
|
||||
|
||||
|
||||
ax0.imshow(image, cmap="gray")
|
||||
ax1.imshow(binary_mask, cmap="gray")
|
||||
|
||||
ax0.set(title="Original image")
|
||||
ax1.set(title="Mask of birdie")
|
||||
|
||||
plt.show()
|
||||
return (image, binary_mask)
|
||||
|
||||
def two_b() -> None:
|
||||
"""
|
||||
Write a function myhist that accepts a grayscale image and the number of bins that
|
||||
will be used in building a histogram. The function should return a 1D array that
|
||||
represents the image histogram (the size should be equal to the number of bins, of
|
||||
course).
|
||||
The histogram is simply a count of pixels with same (or similar) intensity for all
|
||||
bins. You can assume the values of the image are within the interval [0,255]. If you
|
||||
use fewer than 255 bins, intensities will have to be grouped together, e.g. if using 10
|
||||
bins, all values on the interval [0,25] will fall into bin 0.
|
||||
|
||||
Write a script that calculates and displays histograms for different numbers of bins
|
||||
using bird.jpg
|
||||
"""
|
||||
|
||||
|
||||
def main() -> None:
|
||||
excercise_one()
|
||||
#excercise_one()
|
||||
excercise_two()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue