記事ディレクトリ
cv2.imread() | (ファイルパス、フラグ) | 画像を読む |
cv2.imshow() | (画像タイトル、画像オブジェクト) | 表示画像 |
cv2.imwrite() | (ファイル名、画像オブジェクト) | 画像を保存 |
cv2.split() | チャンネル分離 | |
cv2.merge() | チャネルフュージョン | |
cv2.waitKey() | 鍵を待つ | |
cv2.destroyAllWindows() | 窓を破壊する | |
cv2.Canny() | エッジ検出 |
画像読み取り
画像プロパティの読み取り
img = cv2.imread(r'C:\Users\Administrator\Desktop\beginning.jpg')
# 行、列和通道的数量(如果图像是彩色的)
dimensions = img.shape
print(dimensions)
# 图像的大小(图像高度 × 图像宽度 × 图像通道数)
total_number_of_elements= img.size
print(total_number_of_elements)
# 图像的类型
image_dtype = img.dtype
print(image_dtype)
オリジナル画像読み取り
import cv2
img_OpenCV = cv2.imread(r'C:\Users\Administrator\Desktop\beginning.jpg')
cv2.imshow('bgr image', img_OpenCV)
cv2.waitKey(0)
cv2.destroyAllWindows()
グレースケールで読み取る
img_OpenCV = cv2.imread(r'C:\Users\Administrator\Desktop\beginning.jpg',cv2.IMREAD_GRAYSCALE)
或
img_OpenCV = cv2.imread(r'C:\Users\Administrator\Desktop\beginning.jpg',2)
cv2.imread パラメータの説明: cv2.imread(ファイルパス、フラグ)
ファイル パス - 通常は絶対パスを使用します。
flags=1 - 読み取りの前後で、画像形式は変更されません。
flags=2 - 読み取り後、画像形式はグレースケールに変換されます
。 flags=3 - 読み取り後、画像形式に変換されます。 BGR 3 チャンネル画像
RGBチャンネルの変更順序
b, g, r = cv2.split(img_OpenCV)
img_matplotlib = cv2.merge([r, g, b])
特定のチャンネルだけを表示する
img_OpenCV = cv2.imread(r'C:\Users\Administrator\Desktop\beginning.jpg')
# img_OpenCV[:, :, 0]=0
img_OpenCV[:, :, 1]=0
img_OpenCV[:, :, 2]=0
ピクセルのチャネル値を読み取る
img = cv2.imread(r'C:\Users\Administrator\Desktop\beginning.jpg')
(b, g, r) = img[0, 0]
print(b)
print(g)
print(r)
x=img[0, 0,0]
y=img[0, 0,1]
z=img[0, 0,2]
print(x)
print(y)
print(z)
画像の一部を読み取る
画像の前処理
画像のグレースケール
画像のノイズ除去
輪郭認識
OpenCV は、Canny エッジ検出を実現する関数 cv2.Canny() を提供します。その構文は次のとおりです。
edges = cv.Canny( image, threshold1, threshold2[, apertureSize[, L2gradient]])
その内:
edges は計算されたエッジ画像です。
画像は 8 ビット入力画像です。
threshold1 は、プロセスの最初のしきい値を表します。
threshold2 は、プロセスの 2 番目のしきい値を表します。
apertureSize は、Sobel オペレーターのアパーチャ サイズを表します。
L2gradientは画像の勾配の大きさを計算するためのロゴです。デフォルト値は False です。True の場合、より正確な L2 ノルム (つまり、両方向の導関数の平方根) を計算に使用します。それ以外の場合は、L1 ノルム (2 方向の導関数の絶対値を直接加算します) を使用します。
import cv2 as cv
import numpy as np
def line_detect_possible_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
edges = cv.Canny(gray, 50, 150, apertureSize=3) # apertureSize,Canny边缘检测梯度那一步,窗口大小是3
lines = cv.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength=50, maxLineGap=10) #函数将通过步长为1的半径和步长为π/180的角来搜索所有可能的直线
#minLineLength-线的最短长度,比这个线短的都会被忽略
#maxLineGap-两条线之间的最大间隔,如果小于此值,这两条线就会被看成一条线。
for line in lines:
print(type(line))
x1, y1, x2, y2 = line[0]
cv.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv.imshow("line_detect_possible_demo", edges)
src = cv.imread(r'C:\Users\Administrator\Desktop\beginning.jpg') #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
line_detect_possible_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
線検出
import cv2 as cv
import numpy as np
def line_detect_possible_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
edges = cv.Canny(gray, 50, 150, apertureSize=3) # apertureSize,Canny边缘检测梯度那一步,窗口大小是3
lines = cv.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength=50, maxLineGap=10) #函数将通过步长为1的半径和步长为π/180的角来搜索所有可能的直线
#minLineLength-线的最短长度,比这个线短的都会被忽略
#maxLineGap-两条线之间的最大间隔,如果小于此值,这两条线就会被看成一条线。
for line in lines:
print(type(line))
x1, y1, x2, y2 = line[0]
cv.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv.imshow("line_detect_possible_demo", image)
src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/sudoku.png") #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
line_detect_possible_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
ライン交差検出
def lineCrossLine(p1, p2, q1, q2):
def pointAndPointToLine(pt0, pt1): ## 由两点得直线的标准方程 ax+by=c
x0, y0 = pt0
x1, y1 = pt1
return (y1-y0, x0-x1, x0 * y1 - y0 * x1)
a0,b0,c0 = pointAndPointToLine(p1,p2)
a1,b1,c1 = pointAndPointToLine(q1,q2)
dd = a0 * b1 - a1 * b0
if abs(dd) < 1e-6: return None
return ((c0 * b1 - c1 * b0) / dd, (a0 * c1 - a1 * c0) / dd)
if __name__ == '__main__':
print(lineCrossLine((0,0),(1,1),(1,1),(1,0)))
print(lineCrossLine((0,2),(3,0),(3,1),(2,3)))
print(lineCrossLine((0,2),(0,1),(0,1),(2,3)))
print(lineCrossLine((0,2),(3,0),(0,4),(6,0)))
print(lineCrossLine((1,2),(3,5),(1,2),(7,4)))
色の認識
形状認識
デジタル識別
画像ストレージ
cv2.imwrite('data/1.png',img)