python - 使用 CV2 在另一个图像上迭代地应用透明图像覆盖
问题描述
我编写了一些代码,使用 while 循环在 50x50 像素块中迭代 1000x1000 像素图像,并加载先前训练的分类器来预测每个 50x50 块的类别。如果补丁的标签是“0”,我想在那个补丁上画一个矩形,那么最后,所有标签为“0”的补丁都会被突出显示。
import tensorflow as tf
from tensorflow.keras.models import Sequential
import os
import numpy as np
from skimage import io, segmentation, data, color, img_as_float
import cv2
## load keras model
model = tf.keras.models.load_model('./model.h5')
## load image
im = skimage.io.imread('/path/to/image.png')[:,:,0:3]
im_x = im.shape[0]
im_y = im.shape[1]
## rescale image to range 0 to 1
im_rescaled = skimage.img_as_float(im.copy())
x = 0
y = 0
output = im.copy()
alpha = 0.3
while ( y + 50 ) <= im_y:
while ( x + 50 ) <= im_x:
overlay = im.copy()
patch = im_rescaled[x:x+50, y:y+50]
patch_reshaped = np.reshape(patch, ((1, 50, 50, 3)))
y_new = model.predict_classes(patch_reshaped)
if y_new == 0:
overlay = cv2.rectangle(overlay, (x, y), (x+50, y+50), (0, 255, 255), -1)
output = cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output)
x += 50
x = 0
y += 50
skimage.io.imsave('./overlay.png', output)
但是,输出只是原始图像:
解决方案
推荐阅读
- python - How to connect a button with a function of an external class in PyQt?
- python - Change imported modules imports
- npm - NODE_ENV 持有奇怪的价值
- java - What is the efficient way of get members for specific user in LDAP Query?
- javascript - 在功能组件的初始渲染时将变量添加到存储区
- r - How to extract or calcualte special cause variations shown in a qcc chart (xmr, spc)
- c - 如何初始化外部 RAM 中的变量?
- c# - Data visualization from database
- csom - How to fix Error 500 using CSOM before login on sharepoint front-end
- android - XML/android:如何为 RecyclerView 项目制作白色下划线?