Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中图像特定位置的图像水印_Python_Opencv_Image Processing_Watermark - Fatal编程技术网

python中图像特定位置的图像水印

python中图像特定位置的图像水印,python,opencv,image-processing,watermark,Python,Opencv,Image Processing,Watermark,目前,我正在从事一个图像处理项目,在该项目中,我需要将图像分割成几个片段,然后在每个片段上应用水印 我写了一个代码,它通过遮罩将图像分割成若干段。你可以找到代码。现在,我想在每一段上实现水印。可以找到水印教程 我该怎么做? 请帮助我,因为我是OpenCV和Python新手。 请随时询问解决此问题所需的任何进一步信息 谢谢大家! 编辑 我正在为您的推断添加一些代码: ` ` 现在,在保存这些片段之后,我需要通过逐个调用它们来为每个片段添加水印。这是水印的代码: import numpy as np

目前,我正在从事一个图像处理项目,在该项目中,我需要将图像分割成几个片段,然后在每个片段上应用水印

我写了一个代码,它通过遮罩将图像分割成若干段。你可以找到代码。现在,我想在每一段上实现水印。可以找到水印教程

我该怎么做? 请帮助我,因为我是OpenCV和Python新手。 请随时询问解决此问题所需的任何进一步信息

谢谢大家!

编辑

我正在为您的推断添加一些代码: `

`

现在,在保存这些片段之后,我需要通过逐个调用它们来为每个片段添加水印。这是水印的代码:

import numpy as np
import cv2
import os
wk= 'D:\\watermark\\wm.png'
input_im= 'D:\\watermark\\input\\image_01.jpg'
op= 'D:\\watermark\\output'
alpha = 0.25
watermark = cv2.imread(wk, cv2.IMREAD_UNCHANGED)
(wH, wW) = watermark.shape[:2]
image = cv2.imread(input_im)
(h, w) = image.shape[:2]
image = np.dstack([image, np.ones((h, w), dtype="uint8") * 255])
overlay = np.zeros((h, w, 4), dtype="uint8")
overlay[h - wH - 500:h - 500, w - wW - 500:w - 500] = watermark #This is the line where we can set the watermark's coordinates
output = image.copy()
cv2.addWeighted(overlay,alpha, output, 1.0, 0, output)
filename = input_im[input_im.rfind(os.path.sep) + 1:]
p = os.path.sep.join((op, filename))
cv2.imwrite(p, output)
现在,如何提取该段的坐标以对其进行水印

编辑 这是我在排队时得到的

`cv2.circle(im, (cX, cY), 7, (255, 255, 255), -1)
 cv2.putText(im, "center", (cX - 20, cY - 20),
 cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2`
被保持在循环之外:


这就是我在循环中执行它们时得到的结果:

你需要找到图像的计数(我下载了你的分段图像来尝试这个),然后计算轮廓的中心。 要找到轮廓,您需要将图像转换为灰度并对其设置阈值,将全黑色像素(黑色背景)与非黑色像素(您的部分)分开

查找段的中心 我所做的唯一假设是,您分段的像素值不同于0(全黑)。这个假设可能是无效的,但是,由于您正在处理自然景观的照片(如您发布的照片),这应该不是一个问题

请随时询问更多细节

import numpy as np
import cv2

im = cv2.imread('try.png')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,1,255,0)  # Threshold to highlight non black pixels
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
  # compute the center of the contour
  M = cv2.moments(c)
  cX = int(M["m10"] / M["m00"])
  cY = int(M["m01"] / M["m00"])

  # draw the contour and center of the shape on the image
  cv2.drawContours(im, [c], -1, (0, 255, 0), 2)
  cv2.circle(im, (cX, cY), 7, (255, 255, 255), -1)
  cv2.putText(im, "center", (cX - 20, cY - 20),
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

  # show the image
  cv2.imshow("Image", im)
  cv2.waitKey(0)
这就是我得到的:

放置水印 假设有线段区域中心的坐标。知道水印的大小后,您可以将它们转换为坐标,以定位图像中放置水印左上角的点。在本例中,我假设它们是
(x=10,y=10)

我重复使用了你发布的最后一张图片(我不是画轮廓,只是画水印)


您需要创建一个MCVE(请参阅:)。代码资源的外部链接没有用处。@IlarioPierbatista我已经添加了一些详细信息。请检查,做得好。一旦你保存了这些片段,你就有了n张与原始片段尺寸相同的图像(每个片段一张),对吗?如果您没有元数据(例如:带有序列化
掩码
内容的日志文件,这可能导致查找段坐标),则需要通过搜索具有值
!=0
。这听起来对你有好处吗?@IlarioPierbatista我有蒙面内容的坐标。但是如何在坐标区域上实现水印呢?好吧,所以当我尝试适度运行此代码(使其与我的Open CV版本兼容)时,我没有得到与您相同的精确输出。中心点显示在轮廓的右上角。你能编辑你的代码吗?这样我就可以知道哪个部分在for循环中,哪个不在。因为如果这行
cv2.putText(im,“center”(cX-20,cY-20),cv2.FONT\u HERSHEY\u SIMPLEX,0.5,(255,255,255),2)
保留在循环中,图像上会打印出许多中心。嘿,请参考,我使用的是python 2.7和OpenCV 2.4.9,你能不能添加一个屏幕截图来代替我的结果?我已经在你的问题中加入了你正在编辑的内容(在我的答案中添加这个内容会有点混乱)。似乎你的第二部分正在遭受分裂(或类似的事情)。我的建议是从所描述的轮廓列表中只获取较大的区域。让我看看能不能解决这个问题。顺便问一下,你能提供没有绿色轮廓的第二段图像吗?
import numpy as np
import cv2

im = cv2.imread('try.png')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,1,255,0)  # Threshold to highlight non black pixels
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
  # compute the center of the contour
  M = cv2.moments(c)
  cX = int(M["m10"] / M["m00"])
  cY = int(M["m01"] / M["m00"])

  # draw the contour and center of the shape on the image
  cv2.drawContours(im, [c], -1, (0, 255, 0), 2)
  cv2.circle(im, (cX, cY), 7, (255, 255, 255), -1)
  cv2.putText(im, "center", (cX - 20, cY - 20),
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

  # show the image
  cv2.imshow("Image", im)
  cv2.waitKey(0)
import numpy as np 
import cv2 as cv

# Coordinates where to put the watermark (left upper corner)
cy = 10
cx = 10

# Reading the image
image = cv.imread("try.png")
(h,w) = image.shape[:2]
image = np.dstack([image, np.ones((h, w), dtype="uint8") * 255])

# Reading the watermark
watermark = cv.imread("watermark.png", cv.IMREAD_UNCHANGED)
(wH, wW) = watermark.shape[:2]
(B, G, R, A) = cv.split(watermark)
B = cv.bitwise_and(B, B, mask=A)
G = cv.bitwise_and(G, G, mask=A)
R = cv.bitwise_and(R, R, mask=A)
watermark = cv.merge([B, G, R, A])

# Creating the image's overlay with the watermark
overlay = np.zeros((h, w, 4), dtype="uint8")
overlay[cy:wH + cy, cx:wW + cx] = watermark

# Applying the overlay
output = image.copy()
cv.addWeighted(overlay, 0.4, output, 1.0, 0, output)

cv.imshow("out", output)
cv.waitKey()