import cv2 img1 = cv2.imread("F:/tuxiangchuli/shiyan/shiyan1/big.jpg") img2 = cv2.imread("F:/tuxiangchuli/shiyan/shiyan1/smalls.jpg") rows1,cols1,channels1 = img1.shape rows,cols,channels = img2.shape roi = img1[0:rows, (cols1-cols):cols1] img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) ret, mask = cv2.threshold(img2gray, 240, 255, cv2.THRESH_BINARY) mask_inv = cv2.bitwise_not(mask) img1_bg = cv2.bitwise_and(roi, roi, mask=mask) img2_fg = cv2.bitwise_and(img2, img2, mask=mask_inv) dst = cv2.add(img1_bg, img2_fg) img1[0:rows, (cols1-cols):cols1] = dst cv2.imshow("Result", img1) cv2.waitKey(0) cv2.destroyAllWindows() 将以上代码改为将图标放置在左上角
时间: 2024-04-16 09:26:42 浏览: 177
要将图标放置在左上角,只需将 `(cols1-cols):cols1` 的起始和结束位置调整为 `(0:cols)`,使其只覆盖左上角的区域。以下是修改后的代码:
```python
import cv2
img1 = cv2.imread("F:/tuxiangchuli/shiyan/shiyan1/big.jpg")
img2 = cv2.imread("F:/tuxiangchuli/shiyan/shiyan1/smalls.jpg")
rows1, cols1, channels1 = img1.shape
rows, cols, channels = img2.shape
roi = img1[0:rows, 0:cols] # 调整roi的起始和结束位置
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 240, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
img1_bg = cv2.bitwise_and(roi, roi, mask=mask)
img2_fg = cv2.bitwise_and(img2, img2, mask=mask_inv)
dst = cv2.add(img1_bg, img2_fg)
img1[0:rows, 0:cols] = dst # 调整图标放置的位置
cv2.imshow("Result", img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这样修改后的代码将会将图标放置在左上角,希望对您有帮助!
阅读全文