python

时间:2017-06-16 12:22:30

标签: python opencv image-processing watermark

目前我正在处理一个图像处理项目,我需要将图像分成几个部分,然后在每个部分上应用水印。

我编写了一个代码,通过屏蔽将图像分割成段。您可以找到代码here。现在我想在每个细分上实现水印。可以找到水印教程here

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

谢谢!

修改

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

segment= 'segment storing location'
image = cv2.imread(image path)
segments = slic(img_as_float(image),compactness= 100.0, n_segments = 10, sigma = 5)    #segmentation of image
row, col, _ = image.shape
for (i, segVal) in enumerate(np.unique(segments)):
    # construct a mask for the segment
    print "[x] inspecting segment %d" % (i)
    mask = np.zeros(image.shape[:2], dtype = "uint8")
    mask[segments == segVal] = 255 #masking image with different mask to create unique segments
    bb= (cv2.bitwise_and(image, image, mask = mask) )
    cv2.imwrite(segment + str(i) + ".png",bb) #save image segments created

`

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

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)

This is the original image    This is one of its segments  现在,我如何提取该段的坐标以便为其添加水印?

修改

这是我在行

时得到的
`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`

保持在循环之外: uncoordinated centre

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

1 个答案:

答案 0 :(得分:1)

您需要找到图像的计数(我已下载您的段图像以尝试此操作),然后计算轮廓的中心。 要查找轮廓,您需要将图像转换为灰度和阈值,将全黑像素(黑色背景)与非黑像素(您的线段)分开。

查找细分中心

我所做的唯一假设是您的细分的像素值不同于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)

这就是我得到的: Drawing of countours and text centering into them

放置水印

假设您有段区域中心的坐标。知道水印的大小,你可以转换它们坐标,找到图像的位置放置水印的左上角。在这个例子中,我假设它们是(x=10,y=10)

我重复使用了您发布的最后一张图片(我没有画出轮廓,只是水印)。

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()