151 lines
3.4 KiB
Python
151 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
Created on Tue Sep 19 18:17:55 2023
|
||
|
||
@author: ym
|
||
"""
|
||
|
||
import cv2
|
||
import os
|
||
import numpy as np
|
||
|
||
|
||
def tempt_add_adjc():
|
||
|
||
temp = cv2.imread("img.png")
|
||
|
||
path = r"D:\DeepLearning\yolov5\runs\trajectory"
|
||
patr = r"D:\DeepLearning\yolov5\tracking\result"
|
||
for filename in os.listdir(path):
|
||
imgpath = os.path.join(path, filename)
|
||
|
||
img = cv2.imread(imgpath)
|
||
|
||
img1 = cv2.add(img, temp)
|
||
|
||
img1path = os.path.join(patr, filename)
|
||
cv2.imwrite(img1path, img1)
|
||
|
||
def temp_add_boarder():
|
||
temp = cv2.imread("cartedge.png")
|
||
temp[640:, 0:20, :] = 255
|
||
temp[640:, -20:, :] = 255
|
||
temp[-20:, :, :] = 255
|
||
|
||
cv2.imwrite("cartboarder.png", temp)
|
||
|
||
|
||
def create_front_temp():
|
||
image = cv2.imread("./iCart4/b.png")
|
||
Height, Width = image.shape[:2]
|
||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||
thresh, binary = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY_INV)
|
||
board = cv2.bitwise_not(binary)
|
||
contours, _ = cv2.findContours(board, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
|
||
|
||
k = 0
|
||
for cnt in contours:
|
||
img = np.zeros((Height, Width), dtype=np.uint8)
|
||
cv2.drawContours(img, [cnt], -1, 255, 3)
|
||
k += 1
|
||
cv2.imwrite(f"./iCart4/back{k}.png", img)
|
||
|
||
imgshow = cv2.drawContours(image, contours, -1, (0,255,0), 3)
|
||
cv2.imwrite("./iCart4/board_back_line.png", imgshow)
|
||
|
||
# cv2.imwrite("./iCart4/4.png", board)
|
||
# cv2.imwrite("1.png", gray)
|
||
# cv2.imwrite("2.png", binary)
|
||
|
||
|
||
|
||
|
||
def create_back_temp():
|
||
'''
|
||
image1.png:从中获取轮廓的初始图像
|
||
image2.png:主要用于显示效果
|
||
Return:img.png
|
||
'''
|
||
|
||
image = cv2.imread("image1.png")
|
||
|
||
Height, Width = image.shape[:2]
|
||
|
||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||
|
||
gray[:405, :] = 0
|
||
thresh, binary = cv2.threshold(gray, 254, 255, cv2.THRESH_BINARY)
|
||
cv2.imwrite("shopcart.png", binary)
|
||
|
||
imgshow = cv2.cvtColor(binary, cv2.COLOR_GRAY2BGR)
|
||
|
||
|
||
|
||
contours, _ = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
|
||
|
||
imgshow = cv2.drawContours(imgshow, contours, -1, (0,255,0), 1)
|
||
cv2.imwrite("imgshow.png", imgshow)
|
||
|
||
|
||
image2 = cv2.imread("image2.png")
|
||
image2 = cv2.drawContours(image2, contours, -1, (0,255,0), 3)
|
||
|
||
|
||
for cnt in contours:
|
||
_, start, _, num = cv2.boundingRect(cnt)
|
||
|
||
x1 = (cnt[:, 0, 0] != 0)
|
||
x2 = (cnt[:, 0, 0] != Width-1)
|
||
x3 = (cnt[:, 0, 1] != Height-1)
|
||
x = (x1 & x2) & x3
|
||
idx = np.where(x)
|
||
cntx = cnt[idx, :, :][0]
|
||
|
||
cnt1 = cntx[:,0,:].copy()
|
||
|
||
cntx[:, 0, 1] -= 60
|
||
cnt2 = cntx[:,0,:].copy()
|
||
|
||
|
||
cv2.drawContours(image2,[cntx], 0, (0,0,255), 2)
|
||
|
||
|
||
|
||
img = np.zeros(gray.shape, np.uint8)
|
||
for i in range(len(cnt1)):
|
||
x1, y1 = cnt1[i]
|
||
x2, y2 = cnt2[i]
|
||
cv2.rectangle(img, (x1-1, y1-1), (x1+1, y1+1), 255, 1)
|
||
cv2.rectangle(img, (x2-1, y2-1), (x2+1, y2+1), 255, 1)
|
||
|
||
|
||
|
||
cv2.imwrite("img.png", img)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
# create_back_temp()
|
||
# temp_add_boarder()
|
||
# tempt_add_adjc()
|
||
|
||
create_front_temp()
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|