Python處理圖片并實現(xiàn)生成天際線
1、天際線簡介
天際線(SkyLine)顧名思義就是天空與地面的邊界線,人站在不同的高度,會看到不同的景色和地平線,天空與地面建筑物分離的標記線,不得不說,每天抬頭仰望天空,相信大家都可以看到,它的的確確客觀存在,美麗值得欣賞。
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)逐個修改像素點
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] # 計算紅綠藍三波段的平均值
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] # 計算紅綠藍三波段的平均值
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] # 計算紅綠藍三波段的平均值
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 #最開始從每列遍歷從上到下找第一個分界點就停止才用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、運行結(jié)果
3.1 非下采樣+邊緣檢測
python GetSkyLine.py test.jpg 100

原始圖片

邊緣點圖片

邊緣增強圖片

sobel-xy處理后圖片

downloadsampling圖片

紅色邊緣疊加圖片
3.2 下采樣+邊緣檢測
python GetSkyLine.py test.jpg 50 1

原始圖片

邊緣點圖片

邊緣增強圖片

downloadsampling圖片

sobel-xy處理后圖片

紅色邊緣疊加圖片
到此這篇關于Python處理圖片并實現(xiàn)生成天際線的文章就介紹到這了,更多相關Python圖片天際線內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python使用ConfigParser模塊操作配置文件的方法
這篇文章主要介紹了Python使用ConfigParser模塊操作配置文件的方法,結(jié)合實例形式分析了Python基于ConfigParser模塊針對配置文件的創(chuàng)建、讀取、寫入、判斷等相關操作技巧,需要的朋友可以參考下2018-06-06
使用pymysql查詢數(shù)據(jù)庫,把結(jié)果保存為列表并獲取指定元素下標實例
這篇文章主要介紹了使用pymysql查詢數(shù)據(jù)庫,把結(jié)果保存為列表并獲取指定元素下標實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
完美解決TensorFlow和Keras大數(shù)據(jù)量內(nèi)存溢出的問題
這篇文章主要介紹了完美解決TensorFlow和Keras大數(shù)據(jù)量內(nèi)存溢出的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

