欢迎光临散文网 会员登陆 & 注册

Lab Exercise 3

2022-11-08 11:54 作者:温柔的烟火  | 我要投稿

一. 简答题(共1题,100分)

1. (简答题, 100分)

Students are required to submit your solutions before 0:00am on October 31st. You should only upload two files (do not zip), your notebook ".ipynb" file and PDF file generated after notebook execution. 


1. Convert an image to grayscale


Select an interesting RGB image, and convert the image to grayscale according to the following formula 

Y = 0.2126*R + 0.7152*G + 0.0722*B, and plot the result.


2. Alpha Blending


Choose multiple photos as your wish, set one (or several) of them to the background, and the others to the foreground, and use the alpha channel to design the composition formula. For example, output = img1 * alpha1 + img2 * alpha2+.. .+imgN*alphaN, alpha1+alpha2+...+alphaN=1


1) Use your composition formula to generate a new image and display it


2) Explain what effect you expect to achieve by using your composition formula? , what are the characteristics? , what problem did you encounter? How did you solve it.



3. 4-connectivity and m-connectivity labeling 

                                 

试例


Given a binary image as input, the task is to label all white pixels (255) as connected components according to the 4-connectivity and m-connectivity rules. Different areas should be colored differently.


1) Students are required to implement the 4-connectivity and m-connectivity labeling algorithm (It is not allowed to directly or indirectly call third-party functions to complete 4-connectivity and m-connectivity labeling). The algorithm should support at least 100 labels. It takes any binary image as input and outputs the 4-connectivity and m-connectivity labelled images. The output should be similar to the images shown above.


2) Follows the requirements below, verify your algorithm and display the results:


a. Take the following image as input, generate the 4-connectivity and m-connectivity labelled images, and plot them into one row and three columns as the example shown above.

提供的照片


b. Use test_image=generate_image(4,16) to generate a test image, apply your algorithm on it to produce the 4-connectivity and m-connectivity labelled images, and plot them into one row and three columns as the example shown above.


c. Use test_image=generate_image(10,512) to generate a test image, do the same as the step b.


import time

import numpy as np

from skimage import filters


def generate_image(n,l):

    print("n=",n," l=",l)

    print(time.gmtime())

    print(time.time())

    timestamp = int(time.time())

    print(timestamp)

    np.random.seed(timestamp)

    im = np.zeros((l, l))

    points = l * np.random.random((2, n ** 2))

    im[(points[0]).astype(np.int32), (points[1]).astype(np.int32)] = 1

    im = filters.gaussian(im, sigma= l / (5. * n))

    im = im > 0.7 * im.mean()

    return im


3)As shown in the figure below, there is a hole in the region. Please elaborate how to detect a hole in such region? (Students who are capable can try to implement your solution.)

提供
提供




作答(别不完全对,进展示我的)


1. Convert an image to grayscale

Select an interesting RGB image, and convert the image to grayscale according to the following formula

Y = 0.2126R + 0.7152G + 0.0722*B, and plot the result.


import CV2 as cv

import numpy as np

import matplotlib.pyplot as plt

from PIL import Image

import matplotlib.image as image

import os


#展示图片函数

def show(img):

    if img.ndim==2:

        plt.imshow(img,cmap='gray')

    else:

        plt.imshow(cv.cvtColor(img,cv.COLOR_BGR2RGB))

test=cv.imread("Image/date_suqing.png")

show(test)

结果

#使用公式转换图片  首先分离通道

b,g,r = cv.split(test)

img=b*0.0722+g*0.7152+r*0.2126

show(img)

print(test[0])

print(b)

结果


2. Alpha Blending

Choose multiple photos as your wish, set one (or several) of them to the background, and the others to the foreground, and use the alpha channel to design the composition formula. For example, output = img1 * alpha1 + img2 * alpha2+.. .+imgN*alphaN, alpha1+alpha2+...+alphaN=1

1) Use your composition formula to generate a new image and display it

2) Explain what effect you expect to achieve by using your composition formula? , what are the characteristics? , what problem did you encounter? How did you solve it.

#大小规定

def resize_img(img,width,high):

    re=cv.resize(img,(int(width),int(high)))

    return re

#选择3张图片

img1=cv.imread("Image/date_suqing.png")

img2=cv.imread("Image/img2.png")

img3=cv.imread("Image/img3.png")

x,y=img1.shape[0:2]

img2=resize_img(img2,y,x)

img3=resize_img(img3,y,x)

images = [img1,img2,img3]

imgs=np.hstack([img1,img2,img3])

show(imgs)

结果

foreground = img2.astype(float)

foreground2 = img3.astype(float)

background0 = img1.astype(float)

alpha1 = img2.astype(float)/(255*2)

alpha2 = img3.astype(float)/(255*2)


foreground = cv.multiply(alpha1, foreground)

foreground2 = cv.multiply(alpha2, foreground2)

background1 = cv.multiply(1.0 - alpha1, background0)

background2 = cv.multiply(1.0 - alpha2, background0)

outImage = cv.add(foreground, background1)

# cv.imshow("outImg", outImage/255)


# outImage1 = cv.add(foreground2, background2)

# cv.imshow("outImg1", outImage1/255)



outImage2 = cv.add(foreground2,outImage)

cv.imwrite("outimage.jpg",outImage2) 

show(cv.imread('outimage.jpg'))

结果

explain :

期望就是背景放在最后一层,前景在背景基础上能继承,都可以得到较合理的展示,使用相乘的结果,前景取了两张图片,设置两个alpha,来看图片,特点就是,背景图比前景更明显,两张前景图显得隐隐约约,遇到的问题是这样写到底算对不对呢

3. 4-connectivity and m-connectivity labeling

# 4-connectivity

image=cv.imread('Image/464c345f241b858ea278139190664b9.jpg')

show(image)

结果

# 4-connectivity labeling

def four_cc_label(img):

#     print(len(img.shape))

    if(len(img.shape)==3):

    

        height, width, channel = img.shape

    

    else:#查看通道数

                # im 为单通道图像  image为生成的三通道图像

                img = img[:, :, np.newaxis]

                img = img.repeat([3], axis=2)

                height, width, channel = img.shape

    label = np.zeros((height, width), dtype=np.int32)

    LUT = np.zeros(height * width,dtype=np.uint8)

# 色彩列表  支持100个?  ,不如for循环他

    COLORS = np.array([[0, 0, 255], [0, 255, 0], [255, 0, 0],

                       [255, 255, 0], [255, 0, 255], [0, 255, 255],

                       [125, 0, 255], [0, 255, 125], [255, 0, 125], 

                       [255, 255, 125], [255, 125, 255], [0, 125, 255]])

    for ila in range(256):

        s=np.array([[255,0,ila],[125,0,ila]])

        COLORS=np.append(COLORS,s,axis=0)

                      

    COLORS.astype(np.uint8)

    #应该这样支持260左右

    out = np.zeros((height, width, channel), dtype=np.uint8)

    label[img[:,:, 0] > 0] = 1


    n = 1

    for y in range(height):

        for x in range(width):

            if label[y, x] == 0:

                continue

            c3 = label[max(y - 1, 0), x]

            c5 = label[y, max(x - 1, 0)]

            if c3 < 2 and c5 < 2:

                n += 1

                label[y, x] = n

            else:

                _vs = [c3, c5]

                vs = [a for a in _vs if a > 1]

                v = min(vs)

                label[y, x] = v

                minv = v

                for _v in vs:

                    if LUT[_v] != 0:

                        minv = min(minv, LUT[_v])

                for _v in vs:

                    LUT[_v] = minv


    count = 1


    for l in range(2, n + 1):

        flag = True

        for i in range(n + 1):

            if LUT[i] == l:

                if flag:

                    count += 1

                    flag = False

                LUT[i] = count

#     print(len(LUT[2:]))

    for index, lut in enumerate(LUT[2:]):

#         print(lut)

        out[label == (index + 2)] = COLORS[lut - 2]

    return out



#m-connectivity labeling

#find father and update

import CV2

import numpy as np



def four_cc_label2(img):

    if(len(img.shape)==3):

    

        height, width, channel = img.shape

    

    else:#查看通道数

                # im 为单通道图像  image为生成的三通道图像

                img = img[:, :, np.newaxis]

                img = img.repeat([3], axis=2)

                height, width, channel = img.shape

    label = np.zeros((height, width), dtype=np.int32)

    LUT = np.zeros(height * width,dtype=np.uint8)

    label = np.zeros((height, width), dtype=np.int32)

    LUT = np.zeros(height * width,dtype=np.uint8)


    COLORS = np.array([[0, 0, 255], [0, 255, 0], [255, 0, 0],

                       [255, 255, 0], [255, 0, 255], [0, 255, 255]],dtype=np.uint8)

    out = np.zeros((height, width, channel), dtype=np.uint8)

    label[img[:,:, 0] > 0] = 1


    n = 1

    for y in range(height):

        for x in range(width):

            if label[y, x] == 0:

                continue

            c2 = label[max(y - 1, 0), min(x + 1, width - 1)]

            c3 = label[max(y - 1, 0), x]

            c4 = label[max(y - 1, 0), max(x - 1, 0)]

            c5 = label[y, max(x - 1, 0)]

            if c3 < 2 and c5 < 2 and c2 < 2 and c4 < 2:

                n += 1

                label[y, x] = n

            else:

                _vs = [c3, c5, c2, c4]

                vs = [a for a in _vs if a > 1]

                v = min(vs)

                label[y, x] = v


                minv = v

                for _v in vs:

                    if LUT[_v] != 0:

                        minv = min(minv, LUT[_v])

                for _v in vs:

                    LUT[_v] = minv


    count = 1

    for l in range(2, n + 1):

        flag = True

        for i in range(n + 1):

            if LUT[i] == l:

                if flag:

                    count += 1

                    flag = False

                LUT[i] = count



    for i, lut in enumerate(LUT[2:]):

        out[label == (i + 2)] = COLORS[lut - 2]

    return out


prignal=cv.imread('image/bd33bf3400112b949021bfd582ce11df.png')

out = four_cc_label(prignal)

cv.imwrite("outimage2.jpg",out) 

out2=cv.imread('outimage2.jpg')

out5 = four_cc_label2(prignal)

cv.imwrite("outimage5.jpg",out) 

out5=cv.imread('outimage5.jpg')

# show(np.hstack([prignal,out2]))

plt.figure()#创建画布

plt.subplot(1,3,1)

plt.title("binary input")

plt.imshow(prignal)

plt.subplot(1,3,2)

plt.title("4-connectivity labeling")

plt.imshow(out2)

plt.subplot(1,3,3)

plt.title("m-connectivity labeling")

plt.imshow(out5)

plt.show()

结果

import time


import numpy as np



from skimage import filters




def generate_image(n,l):


    print("n=",n," l=",l)


    print(time.gmtime())


    print(time.time())


    timestamp = int(time.time())


    print(timestamp)


    np.random.seed(timestamp)


    im = np.zeros((l, l))


    points = l * np.random.random((2, n ** 2))


    im[(points[0]).astype(np.int32), (points[1]).astype(np.int32)] = 1


    im = filters.gaussian(im, sigma= l / (5. * n))


    im = im > 0.7 * im.mean()


    return im

test=generate_image(4,16)

#将二维图像变为三维但不影响其图像显示

out = four_cc_label(test)

cv.imwrite("outimage3.jpg",out) 

out3=cv.imread('outimage3.jpg')


out = four_cc_label2(test)

cv.imwrite("outimage6.jpg",out) 

out6=cv.imread('outimage6.jpg')

plt.figure()#创建画布

plt.subplot(1,3,1)

plt.title("binary input")

plt.imshow(test)

plt.subplot(1,3,2)

plt.title("4-connectivity labeling")

plt.imshow(out3)

plt.subplot(1,3,3)

plt.title("m-connectivity labeling")

plt.imshow(out6)

plt.show()

结果

#c.Use test_image=generate_image(10,512) to generate a test image, do the same as the step b.





test=generate_image(10,512)

#将二维图像变为三维但不影响其图像显示

out = four_cc_label(test)

cv.imwrite("outimage4.jpg",out) 

out4=cv.imread('outimage4.jpg')

plt.figure()#创建画布

plt.subplot(1,2,1)

plt.title("binary input")

plt.imshow(test)

plt.subplot(1,2,2)

plt.title("4-connectivity labeling")

plt.imshow(out4)

plt.show()

结果


#3)As shown in the figure below, there is a hole in the region. Please elaborate how to detect a hole in such region? (Students who are capable can try to implement your solution.)


#使用4领域标注法检测出一段差异的位置,即可标注出图像空白的部分


Lab Exercise 3的评论 (共 条)

分享到微博请遵守国家法律