Lab Exercise 1
一. 简答题(共1题,100分)
1. (简答题)
Students are required to submit the solutions of 4th, 5th and 6th before 0:00am on September 19th. You should only upload two files (do not zip), your notebook ".ipynb" file and PDF file generated after notebook execution.
You may follow this instructions to finish the lab execise:
1.Confirm whether anaconda is installed. If not, please go to the official website https://www.anaconda.com/ and follow the installation instructions there.
2. Use conda to create a virtual environment named image_processing_2022. (Note: If create image_processing_2022 by cloning the base environment it may save lots of efforts to download and install default modules.)
3. Activate the image_processing_2022 virtual environment, install scikit-image (or OpenCV, etc., you can choose freely) and necessary dependent modules in this environment.
4. Use skimage/opencv to read any image from the local hard disk and display it in the notebook (you can use matplotlib)
5. Display the images in red, green and blue single grayscale channel respectively (three grayscale images), and then display them in red, green and blue multi-channels separately (three RGB images, each containing only one non-zero color channel), and then perform an invert color operation on each non-zero color channel, such as the red channel Red(x,y)=255−Red(x,y). Together with the original image, Display four images in a 2x2 grid. The result should be similar as follows:

6. Use the mask to draw the image into a picture frame (preferably not a simple shape, e.g. rectangle), the picture frame only shows a part of the original picture (consider using the alpha channel of RGBA, preferably not white). Explain how you did it.

给的示例
答案(并不是完全对,仅供参考优化)
from skimage import io,data,color
image=io.imread("D:\\pythonProject\\image\\com\\monan\\image\\origanal.png")
io.imshow(image)

import CV2
import numpy as np
import matplotlib.pyplot as plt
from skimage import io,data,color
image=CV2.imread("D:\\pythonProject\\image\\com\\monan\\image\\origanal.png")
image4=io.imread("D:\\pythonProject\\image\\com\\monan\\image\\origanal.png")
B,G,R= CV2.split(image)
#CV2.imshow("Blue", B)
# CV2.imshow("Green", G)
# CV2.imshow("Red", R)
# CV2.waitKey()
# CV2.destroyAllWindows()
CV2.imwrite("D:\\pythonProject\\image\\com\\monan\\image\\B-image.png",B)
CV2.imwrite("D:\\pythonProject\\image\\com\\monan\\image\\R-image.png",R)
CV2.imwrite("D:\\pythonProject\\image\\com\\monan\\image\\G-image.png",G)
image_b=io.imread("D:\\pythonProject\\image\\com\\monan\\image\\B-image.png")
io.imshow(image_b)

image_r=io.imread("D:\\pythonProject\\image\\com\\monan\\image\\R-image.png")
io.imshow(image_r)

image_g=io.imread("D:\\pythonProject\\image\\com\\monan\\image\\G-image.png")
io.imshow(image_g)

blue = np.zeros_like(image)
blue[..., 0] = image[..., 0]
# CV2.imshow('blue', blue)
green = np.zeros_like(image)
green[..., 1] = image[..., 1]
# CV2.imshow('green', green)
red = np.zeros_like(image)
red[..., 2] = image[..., 2]
# CV2.imshow('red', red)
#对上面的图像进行单色反色操作
h, w, channels = blue.shape[0:3]
# print(h,w,channels)
for row in range(h):
for col in range(w):
pixel = image[row, col, 0]
blue[row, col, 0] = 255-pixel
# CV2.imshow("blue",blue)
h, w, channels = red.shape[0:3]
for row in range(h):
for col in range(w):
pixel = image[row, col, 2]
red[row, col, 2] = 255-pixel
# CV2.imshow("red",red)
h, w, channels = green.shape[0:3]
for row in range(h):
for col in range(w):
pixel = image[row, col, 1]
green[row, col, 1] = 255-pixel
# CV2.imshow("green",green)
CV2.imwrite("D:\\pythonProject\\image\\com\\monan\\image\\blue-translate.png",blue)
CV2.imwrite("D:\\pythonProject\\image\\com\\monan\\image\\red-translate.png",red)
CV2.imwrite("D:\\pythonProject\\image\\com\\monan\\image\\green-translate.png",green)
plt.figure()#创建画布
#image是原始图像
#但是是空白的
image1=io.imread("D:\\pythonProject\\image\\com\\monan\\image\\blue-translate.png")
image2=io.imread("D:\\pythonProject\\image\\com\\monan\\image\\red-translate.png")
image3=io.imread("D:\\pythonProject\\image\\com\\monan\\image\\green-translate.png")
plt.subplot(2,2,1)
plt.imshow(image4)
plt.subplot(2,2,2)
plt.imshow(image1)
plt.subplot(2,2,3)
plt.imshow(image2)
plt.subplot(2,2,4)
plt.imshow(image3)
plt.show()

coordinates = [[[40,20], [120,30],[140,60], [85,110], [35,120]]]
coordinates = np.array(coordinates)
print(coordinates)
mask1 = np.zeros(img.shape[:2], dtype=np.int8)
print(mask1)
mask1 = cv.fillPoly(mask1, coordinates,255)
print(mask1)
print(img.shape[:2])
show(mask1)

import CV2 as cv
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import pycocotools.mask as mask_util
def show(img):
if img.ndim==2:
plt.imshow(img,cmap='gray')
else:
plt.imshow(cv.cvtColor(img,cv.COLOR_BGR2RGB))
plt.show()
img = cv.imread('image/origanal.png')
# img.shape (127, 161, 3)
mask_threth = 50
# 先画掩膜
coordinates = [[[40,20], [120,30],[140,60], [85,110], [35,120]]]
coordinates = np.array(coordinates)
mask1 = np.zeros(img.shape[:2], dtype=np.int8)
mask1 = cv.fillPoly(mask1, coordinates, 255)
show(mask1)

image = cv.add(img, np.zeros(np.shape(img), dtype=np.uint8), mask=mask1)
show(image)

Explain how did it
You import the image, then create a two-dimensional matrix to store the coordinates, and then get the width and height of the image to be processed。Using fillPoly,fill the imaginary border of the point you want to depict with black to form images.Using image motion ,add the two pictures togetheraddition。Because the pictures are only 255 or 0 ,so the overlapping black areas must be black, and the white brush areas remain as they are