简体   繁体   English

提取像素坐标并粘贴到新图像python上

[英]Extract pixel coordinates and paste on new image python

图片

This code is returning pixels coordinates which have red color now i want to extract and paste those pixels on new image. 这段代码返回的像素坐标红色,现在我要提取这些像素并将其粘贴到新图像上。 how do i paste pixel coordinates? 如何粘贴像素坐标? Please ask if question is not clear. 请询问是否不清楚。

import cv2
import numpy as np
filename = "oOHc6.png"
img = cv2.imread(filename, 1)
hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hsv_lower=np.uint8([0, 200, 210])
hsv_upper=np.uint8([180, 250, 250])
mask= cv2.inRange(hsv, hsv_lower, hsv_upper)

#display mask
res = cv2.bitwise_and(img,img,mask = mask)
res_gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
ys,xs = np.where(res_gray>0)
pts = [(x,y) for x,y in zip(xs,ys)]

empty = np.zeros_like(img)
mask_c = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
imaskc = mask_c>0

empty[imaskc] = img[imaskc]
#empty.save('C:/Python27/cclabel/images/NewImage'+'.png','png')
cv2.imwrite("new.png", empty)

I do cv2.inRange() in HSV-space to get the mask for the red region: 我在HSV空间中执行cv2.inRange()以获取红色区域的蒙版:

在此处输入图片说明

Then use the mask-operation(such as cv2.bitwise_and()/np.where()/ slices) to "paste" to another image. 然后使用mask操作(例如cv2.bitwise_and()/ np.where()/ slices)“粘贴”到另一个图像。

在此处输入图片说明


To get the coords, you can also use the np.where() like that. 要获取坐标,您还可以像这样使用np.where()。

# 使用 cv2.bitwise_and 掩模操作,然后使用 np.where 获取坐标
res = cv2.bitwise_and(img,img,mask = mask)
res_gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
ys,xs = np.where(res_gray>0)
pts = [(x,y) for x,y in zip(xs,ys)]

To "copy-paste" into another same size image: 要将“复制粘贴”到另一个相同大小的图像中:

## 复制-粘贴到其他空白的地方
empty = np.zeros_like(img)
mask_c = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
imaskc = mask_c>0
empty[imaskc] = img[imaskc]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python 中从图像中提取每个像素的 x,y 坐标 - Extract x,y coordinates of each pixel from an image in Python 协调tkinter画布和PIL图像上的像素坐标(Python) - Reconciling pixel coordinates on tkinter canvas and PIL image (Python) 在 Python 中转换图像,为每个像素指定目标坐标 - Transform an image in Python specifying target coordinates for each pixel 在 python 中模板匹配后如何确定图像上的像素坐标? - How to determine pixel coordinates on an image after template matching in python? 从 Python 中的二进制图像获取像素边界坐标(不是边缘) - Get pixel boundary coordinates from binary image in Python (not edges) 如何从 matplotlib-pyplot 生成的图像中提取图像像素坐标到 2 元组列表? - How to extract image pixel coordinates to a list of 2 tuple from matplotlib-pyplot-generated image? 查找图像中红色像素的坐标 - Find the coordinates of red pixel in an image 使用 opencv 获取由椭圆包围的图像区域中的像素坐标和像素值 python - Getting pixel coordinates and pixel values in image region bounded by an ellipse using opencv python 使用掩码从特定坐标处的图像(2d数组)中提取像素值 - Extract pixel values from image (2d array) at specific coordinates using mask 使用 Python,是否可以遍历栅格并为每个像素提取纬度/经度坐标? - Using Python, is it possible loop through a raster and extract Lat/Long coordinates for each pixel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM