最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

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?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

試?yán)?/figcaption>


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.)

提供
提供




作答(別不完全對,進(jìn)展示我的)


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


#展示圖片函數(shù)

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)

結(jié)果

#使用公式轉(zhuǎn)換圖片? 首先分離通道

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

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

show(img)

print(test[0])

print(b)

結(jié)果


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.

#大小規(guī)定

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)

結(jié)果

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'))

結(jié)果

explain :

期望就是背景放在最后一層,前景在背景基礎(chǔ)上能繼承,都可以得到較合理的展示,使用相乘的結(jié)果,前景取了兩張圖片,設(shè)置兩個alpha,來看圖片,特點(diǎn)就是,背景圖比前景更明顯,兩張前景圖顯得隱隱約約,遇到的問題是這樣寫到底算對不對呢

3. 4-connectivity and m-connectivity labeling

# 4-connectivity

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

show(image)

結(jié)果

# 4-connectivity labeling

def four_cc_label(img):

#? ? ?print(len(img.shape))

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

? ??

? ? ? ? height, width, channel = img.shape

? ??

? ? else:#查看通道數(shù)

? ? ? ? ? ? ? ? # 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循環(huán)他

? ? 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)

? ? #應(yīng)該這樣支持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:#查看通道數(shù)

? ? ? ? ? ? ? ? # 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()#創(chuàng)建畫布

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()

結(jié)果

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)

#將二維圖像變?yōu)槿S但不影響其圖像顯示

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()#創(chuàng)建畫布

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()

結(jié)果

#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)

#將二維圖像變?yōu)槿S但不影響其圖像顯示

out = four_cc_label(test)

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

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

plt.figure()#創(chuàng)建畫布

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()

結(jié)果


#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領(lǐng)域標(biāo)注法檢測出一段差異的位置,即可標(biāo)注出圖像空白的部分


Lab Exercise 3的評論 (共 條)

分享到微博請遵守國家法律
盐源县| 镇宁| 乌拉特后旗| 行唐县| 威海市| 三明市| 安乡县| 芦溪县| 无极县| 师宗县| 常德市| 呼和浩特市| 沙湾县| 浪卡子县| 尖扎县| 昂仁县| 新余市| 新绛县| 扶绥县| 康平县| 黔西县| 衢州市| 班戈县| 通榆县| 三台县| 敦化市| 广元市| 安康市| 壶关县| 西林县| 桃园县| 辽阳市| 麻城市| 余庆县| 盐山县| 聊城市| 南川市| 天水市| 朝阳区| 永吉县| 沭阳县|