最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python OpenCV實(shí)現(xiàn)測量圖片物體寬度

 更新時(shí)間:2020年05月27日 14:38:47   作者:shinebay  
這篇文章主要介紹了Python OpenCV實(shí)現(xiàn)測量圖片物體寬度,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、 題目描述

測量所給圖片的高度,即上下邊緣間的距離。

思路:

  • 將圖片進(jìn)行閾值操作得到二值化圖片。
  • 截取只包含上下邊框的部分,以便于后續(xù)的輪廓提取
  • 輪廓檢測
  • 得到結(jié)果

二、 實(shí)現(xiàn)過程

1.用于給圖片添加中文字符

#用于給圖片添加中文字符
def ImgText_CN(img, text, left, top, textColor=(0, 255, 0), textSize=20):
  if (isinstance(img, np.ndarray)): #判斷是否為OpenCV圖片類型
    img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
  draw = ImageDraw.Draw(img)
  fontText = ImageFont.truetype(r'C:\Windows\Fonts\simsun.ttc', textSize, encoding="utf-8")     ##中文字體
  draw.text((left, top), text, textColor, font=fontText)   #寫文字
  return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)

2.實(shí)現(xiàn)圖片反色功能

#實(shí)現(xiàn)圖片反色功能
def PointInvert(img):
  height, width = img.shape    #獲取圖片尺寸
  for i in range(height):
    for j in range(width):
      pi = img[i, j]
      img[i, j] = 255 - pi
  return img

3.邊緣檢測

# canny邊緣檢測
edges = cv2.Canny(th, 30, 70) 
res=PointInvert(edges)              #顏色反轉(zhuǎn)
#顯示圖片
cv2.imshow('original', th)            #顯示二值化后的圖,主題為白色,背景為黑色 更加容易找出輪廓
key = cv2.waitKey(0)
if key==27: #按esc鍵時(shí),關(guān)閉所有窗口
  print(key)
  cv2.destroyAllWindows()

4.輪廓操作

contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)    #得到輪廓

cnt = contours[0]        #取出輪廓

x, y, w, h = cv2.boundingRect(cnt)     #用一個(gè)矩形將輪廓包圍

img_gray = cv2.cvtColor(res, cv2.COLOR_GRAY2BGR)        #將灰度轉(zhuǎn)化為彩色圖片方便畫圖

cv2.line(img_gray, (x, y), (x + w, y), (0,0,255), 2, 5)     #上邊緣
cv2.line(img_gray, (x, y+h), (x + w, y+h), (0, 0, 255), 2, 5)  #下邊緣

img1[80:230, 90:230] = img_gray     #用帶有上下輪廓的圖替換掉原圖的對(duì)應(yīng)部分

5.顯示圖片

res1=ImgText_CN(img1, '寬度%d'%h, 25, 25, textColor=(0, 255, 0), textSize=30)  #繪制文字
#顯示圖片 
cv2.imshow('original', res1)
key = cv2.waitKey(0)
if key==27: #按esc鍵時(shí),關(guān)閉所有窗口
  print(key)
  cv2.destroyAllWindows()

6.完整代碼

import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont

#用于給圖片添加中文字符
def ImgText_CN(img, text, left, top, textColor=(0, 255, 0), textSize=20):
  if (isinstance(img, np.ndarray)): #判斷是否為OpenCV圖片類型
    img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
  draw = ImageDraw.Draw(img)
  fontText = ImageFont.truetype(r'C:\Windows\Fonts\simsun.ttc', textSize, encoding="utf-8")     ##中文字體
  draw.text((left, top), text, textColor, font=fontText)   #寫文字
  return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)

#實(shí)現(xiàn)圖片反色功能
def PointInvert(img):
  height, width = img.shape    #獲取圖片尺寸
  for i in range(height):
    for j in range(width):
      pi = img[i, j]
      img[i, j] = 255 - pi
  return img



img=cv2.imread("gongjian1.bmp",0)        #加載彩色圖
img1=cv2.imread("gongjian1.bmp",1)        #加載灰度圖

recimg = img[80:230, 90:230]          #截取需要的部分
img2 = img1[80:230, 90:230]           #截取需要的部分
ret, th = cv2.threshold(recimg, 90, 255, cv2.THRESH_BINARY)     #閾值操作二值化


# canny邊緣檢測
edges = cv2.Canny(th, 30, 70) 
res=PointInvert(edges)              #顏色反轉(zhuǎn)
#顯示圖片
cv2.imshow('original', th)            #顯示二值化后的圖,主題為白色,背景為黑色 更加容易找出輪廓
key = cv2.waitKey(0)
if key==27: #按esc鍵時(shí),關(guān)閉所有窗口
  print(key)
  cv2.destroyAllWindows()
  
contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)    #得到輪廓

cnt = contours[0]        #取出輪廓

x, y, w, h = cv2.boundingRect(cnt)     #用一個(gè)矩形將輪廓包圍

img_gray = cv2.cvtColor(res, cv2.COLOR_GRAY2BGR)        #將灰度轉(zhuǎn)化為彩色圖片方便畫圖

cv2.line(img_gray, (x, y), (x + w, y), (0,0,255), 2, 5)     #上邊緣

cv2.line(img_gray, (x, y+h), (x + w, y+h), (0, 0, 255), 2, 5)  #下邊緣
img1[80:230, 90:230] = img_gray                 #用帶有上下輪廓的圖替換掉原圖的對(duì)應(yīng)部分

res1=ImgText_CN(img1, '寬度%d'%h, 25, 25, textColor=(0, 255, 0), textSize=30)  #繪制文字
#顯示圖片 
cv2.imshow('original', res1)
key = cv2.waitKey(0)
if key==27: #按esc鍵時(shí),關(guān)閉所有窗口
  print(key)
  cv2.destroyAllWindows()

三、 運(yùn)行結(jié)果(效果)

四、 問題及解決方法

紅色輪廓沒有顯示,解決方案:將灰度圖轉(zhuǎn)化為彩色圖

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

宜章县| 淳化县| 峨眉山市| 康定县| 策勒县| 广丰县| 柏乡县| 高碑店市| 宁城县| 钟祥市| 临夏市| 汝阳县| 麻栗坡县| 浪卡子县| 区。| 永顺县| 开封市| 广州市| 武川县| 淄博市| 诸暨市| 陕西省| 桐柏县| 水城县| 栖霞市| 贵南县| 微山县| 黄梅县| 柞水县| 东港市| 英超| 雷波县| 三河市| 佛冈县| 山东省| 慈溪市| 防城港市| 张掖市| 固始县| 巴林右旗| 什邡市|