Python處理圖片并實(shí)現(xiàn)生成天際線
1、天際線簡(jiǎn)介
天際線(SkyLine
)顧名思義就是天空與地面的邊界線,人站在不同的高度,會(huì)看到不同的景色和地平線,天空與地面建筑物分離的標(biāo)記線,不得不說(shuō),每天抬頭仰望天空,相信大家都可以看到,它的的確確客觀存在,美麗值得欣賞。
2、Python代碼
#-*- coding:utf-8 -*- import sys from os.path import exists import cv2 import numpy as np def getImage(height, width, channels): image = np.zeros([height, width, 3], np.uint8) # 三通道順序是BGR # 三層循環(huán)逐個(gè)修改像素點(diǎn) for row in range(height): for col in range(width): for c in range(channels): image[row, col, c] = 0 return image def isWhite(pixel_value, threshold): #閾值可以取10、20、30、50、100 res = False if pixel_value[0] > threshold and pixel_value[1] > threshold and pixel_value[2] > threshold: # 10、10、10 50、50、50 這里是天空和地面樓山的分界線,需要調(diào)參 res = True return res def isPureWhite(pixel_value): res = False if pixel_value[0] == 255 and pixel_value[1] == 255 and pixel_value[2] == 255: # >3|>3|>3 10、10、10 res = True return res def getRowNumberSpecificCol(image, col): res_row = -1 height, width = image.shape[0:2] if col >= 0 and col < width: for row in range(0, height): pv = image[row][col] if(pv[0] > 0 and pv[1] > 0 and pv[2] >0): res_row = row break return res_row def getEnhancedEdgeImageFromEdgeImage(edge_Image): edge_SrcImage = edge_Image height, width = edge_SrcImage.shape[0:2] for col in range(1, width): for row in range(0, height): pixel_value = edge_SrcImage[row][col] # 計(jì)算紅綠藍(lán)三波段的平均值 if isPureWhite(pixel_value): r_last = getRowNumberSpecificCol(edge_SrcImage, col - 1) if r_last: if row > r_last: minR, maxR = r_last, row for k in range(minR, maxR): edge_SrcImage[k][col - 1][0] = 255 edge_SrcImage[k][col - 1][1] = 255 edge_SrcImage[k][col - 1][2] = 255 else: minR, maxR = row, r_last for k in range(minR, maxR): edge_SrcImage[k][col][0] = 255 edge_SrcImage[k][col][1] = 255 edge_SrcImage[k][col][2] = 255 # cv2.imshow("Enhanced-edge-image", edge_SrcImage) return edge_SrcImage def getFileExtensionname(filename): res = ".png" dot_index = -1 for i in range(len(filename), 0): if filename[i] == '.': dot_index = i break if dot_index != -1: res = filename[dot_index: len(filename)-1] return res if __name__ == '__main__': origin_pic_filename = "D:/test.png" sky_ground_threshold = 30 isDownSampling = False if (len(sys.argv) == 1): print(sys.argv[0]) origin_pic_filename = "" elif(len(sys.argv) == 2): origin_pic_filename = str(sys.argv[1]) elif(len(sys.argv) == 3): origin_pic_filename = str(sys.argv[1]) sky_ground_threshold = int(sys.argv[2]) elif (len(sys.argv) == 4): origin_pic_filename = str(sys.argv[1]) sky_ground_threshold = int(sys.argv[2]) if(int(sys.argv[3]) == 1): isDownSampling = True if origin_pic_filename != "" and sky_ground_threshold > 0: print(("輸入圖片文件名為:{0}").format(origin_pic_filename)) print(("天空地面分界灰度閾值為:{0}").format(sky_ground_threshold)) suffix_name = getFileExtensionname(origin_pic_filename) print(("后綴名為:{0}").format(suffix_name)) srcImage = cv2.imread(origin_pic_filename) inputSrcImage = srcImage if isDownSampling: inputSrcImage = cv2.pyrDown(inputSrcImage) height, width = inputSrcImage.shape[0:2] print(("高度:{0}, 寬度:{1}").format(height, width)) cv2.namedWindow('downsampling-image', cv2.WINDOW_AUTOSIZE) cv2.imshow("downsampling-image", inputSrcImage) Sobelx = cv2.Sobel(inputSrcImage, cv2.CV_64F, 1, 0) Sobely = cv2.Sobel(inputSrcImage, cv2.CV_64F, 0, 1) Sobelx = cv2.convertScaleAbs(Sobelx) Sobely = cv2.convertScaleAbs(Sobely) # cv2.imshow("sobel-x-Abs", Sobelx) # cv2.imshow("sobel-y-Abs", Sobely) Sobelxy = cv2.addWeighted(Sobelx, 0.5, Sobely, 0.5, 0) cv2.namedWindow('sobel-xy', cv2.WINDOW_AUTOSIZE) cv2.imshow('sobel-xy', Sobelxy) edgeImage = getImage(height, width, 3) for col in range(0, width): for row in range(0, height): pixel_value = Sobelxy[row][col] # 計(jì)算紅綠藍(lán)三波段的平均值 if isWhite(pixel_value, sky_ground_threshold): edgeImage[row][col][0] = 255 edgeImage[row][col][1] = 255 edgeImage[row][col][2] = 255 break cv2.namedWindow('edge-image', cv2.WINDOW_AUTOSIZE) cv2.imshow('edge-image', edgeImage) cv2.imwrite(origin_pic_filename.replace(suffix_name, "-ZGetEdge.png"), edgeImage) enhanced_edgeImage = getEnhancedEdgeImageFromEdgeImage(edgeImage) cv2.namedWindow('enhanced-edge-image', cv2.WINDOW_AUTOSIZE) cv2.imshow('enhanced-edge-image', enhanced_edgeImage) cv2.imwrite(origin_pic_filename.replace(suffix_name, "-EnhancedEdge.png"), enhanced_edgeImage) for col in range(0, width): for row in range(0, height): pixel_value = enhanced_edgeImage[row][col] # 計(jì)算紅綠藍(lán)三波段的平均值 if isPureWhite(pixel_value): if row+2 < height: inputSrcImage[row+2][col][0] = 0 inputSrcImage[row+2][col][1] = 0 inputSrcImage[row+2][col][2] = 255 else: inputSrcImage[row][col][0] = 0 inputSrcImage[row][col][1] = 0 inputSrcImage[row][col][2] = 255 # inputSrcImage[row][col][0] = 0 # inputSrcImage[row][col][1] = 0 # inputSrcImage[row][col][2] = 255 # break #最開(kāi)始從每列遍歷從上到下找第一個(gè)分界點(diǎn)就停止才用break cv2.namedWindow('RedEdge-image', cv2.WINDOW_AUTOSIZE) cv2.imshow('RedEdge-image', inputSrcImage) cv2.imwrite(origin_pic_filename.replace(suffix_name, "-RedEdge.png"), inputSrcImage) cv2.waitKey(0) cv2.destroyAllWindows() print('Success!') cv2.waitKey() cv2.destroyAllWindows()
3、運(yùn)行結(jié)果
3.1 非下采樣+邊緣檢測(cè)
python GetSkyLine.py test.jpg 100
原始圖片
邊緣點(diǎn)圖片
邊緣增強(qiáng)圖片
sobel-xy處理后圖片
downloadsampling圖片
紅色邊緣疊加圖片
3.2 下采樣+邊緣檢測(cè)
python GetSkyLine.py test.jpg 50 1
原始圖片
邊緣點(diǎn)圖片
邊緣增強(qiáng)圖片
downloadsampling圖片
sobel-xy處理后圖片
紅色邊緣疊加圖片
到此這篇關(guān)于Python處理圖片并實(shí)現(xiàn)生成天際線的文章就介紹到這了,更多相關(guān)Python圖片天際線內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python利用scatter繪畫(huà)散點(diǎn)圖
這篇文章主要介紹了python利用scatter繪畫(huà)散點(diǎn)圖,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助2022-06-06Python使用ConfigParser模塊操作配置文件的方法
這篇文章主要介紹了Python使用ConfigParser模塊操作配置文件的方法,結(jié)合實(shí)例形式分析了Python基于ConfigParser模塊針對(duì)配置文件的創(chuàng)建、讀取、寫(xiě)入、判斷等相關(guān)操作技巧,需要的朋友可以參考下2018-06-06使用pymysql查詢數(shù)據(jù)庫(kù),把結(jié)果保存為列表并獲取指定元素下標(biāo)實(shí)例
這篇文章主要介紹了使用pymysql查詢數(shù)據(jù)庫(kù),把結(jié)果保存為列表并獲取指定元素下標(biāo)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05完美解決TensorFlow和Keras大數(shù)據(jù)量?jī)?nèi)存溢出的問(wèn)題
這篇文章主要介紹了完美解決TensorFlow和Keras大數(shù)據(jù)量?jī)?nèi)存溢出的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07如何用Python畫(huà)一些簡(jiǎn)單形狀你知道嗎
這篇文章主要介紹了用Python作圖的一個(gè)簡(jiǎn)單實(shí)例,通過(guò)turtle模塊實(shí)現(xiàn)作圖,具有一定參考價(jià)值,需要的朋友可以了解下希望能給你帶來(lái)幫助2021-08-08tensorflow實(shí)現(xiàn)圖像的裁剪和填充方法
今天小編就為大家分享一篇tensorflow實(shí)現(xiàn)圖像的裁剪和填充方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07解決c++調(diào)用python中文亂碼問(wèn)題
這篇文章主要介紹了c++調(diào)用python中文亂碼問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07