【问题标题】:How to encode Grayscale, SobelX and SobelY in one RGB image using OpenCV?如何使用 OpenCV 在一张 RGB 图像中对灰度、SobelX 和 SobelY 进行编码?
【发布时间】:2020-10-25 17:59:26
【问题描述】:

我有一个 RGB 图像。我想将其另存为新图像,其中GrayscaleSobelX and SobelY 将保存在新图像的 R、G 和 B 通道中。如何在 OpenCV 中做这样的事情?

换句话说,假设我们有 RBG image,我们想创建一个新的 RGB(或 BGR 无关紧要)图像,该图像将在其通道中包含灰度值(在 B 中)、sobelX(在 R 中)sobelY (在 G)。主要问题是我们需要以某种方式将 Sobel 量化\归一化为 0-256 值......如何做这样的事情?

感谢@Rabbid79 最终得到:

%matplotlib inline
from matplotlib import pyplot as plt
import cv2
import numpy as np
!wget "https://helpx.adobe.com/content/dam/help/en/stock/how-to/visual-reverse-image-search/jcr_content/main-pars/image/visual-reverse-image-search-v2_intro.jpg" -O dt.jpg

src = cv2.imread('./dt.jpg', cv2.IMREAD_GRAYSCALE)
def show(im):
  plt.imshow(im)
  plt.show()

show(src)
sobelx = cv2.Sobel(src, cv2.CV_64F, 1, 0)
sobely = cv2.Sobel(src, cv2.CV_64F, 0, 1)

abs_grad_x = cv2.convertScaleAbs(sobelx)
abs_grad_y = cv2.convertScaleAbs(sobely)
grad = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
show(grad)
b = cv2.GaussianBlur(src,(3,3),0)
laplacian = cv2.Laplacian(b,cv2.CV_64F)
l_dst = cv2.convertScaleAbs( laplacian  );
show(l_dst)
dest = np.dstack([src, l_dst, grad]).astype(np.uint8)
show(dest)

【问题讨论】:

  • 你知道如何形成这 3 个东西,并且只想知道如何组合这 3 个结果吗?您打算使用哪个渠道制作 Sobel 图像?我可以问一下为什么您要这样做以及您打算如何处理结果?
  • 你可以使用cv2.merge()
  • @MarkSetchell 我觉得它看起来很迷幻!也许它只是为了保存图像,但即使它是一个错误并且没有达到 OP 的意图,它也会很有趣,甚至可能很有见地。
  • @beaker 我喜欢你的积极态度????

标签: python numpy opencv cv2


【解决方案1】:

将图像加载为灰度图像(IMREAD_GRAYSCALE):

gray = cv2.imread(image_name, cv2.IMREAD_GRAYSCALE)

分别创建一个sobel-X和sobel-Y

sobelx = cv2.convertScaleAbs((cv2.Sobel(gray, cv2.CV_64F, 1, 0)))
sobely = cv2.convertScaleAbs((cv2.Sobel(gray, cv2.CV_64F, 0, 1)))

用源图像的大小创建一个空的numpy数组,并将灰度、sobel-X和sobel-Y分配给目标图像的通道:

dest = np.empty((gray.shape[0], gray.shape[1], 3), np.uint8)
dest[:,:,0] = gray
dest[:,:,1] = sobelx
dest[:,:,2] = sobely

或者merge图片:

dest = cv2.merge((gray, sobelx, sobely))

分别使用numpy.dstack:

dest = np.dstack([gray, sobelx, sobely]).astype(np.uint8)

大家一起:

gray   = cv2.imread(image_name, cv2.IMREAD_GRAYSCALE)
sobelx = cv2.convertScaleAbs((cv2.Sobel(gray, cv2.CV_64F, 1, 0)))
sobely = cv2.convertScaleAbs((cv2.Sobel(gray, cv2.CV_64F, 0, 1)))
dest   = np.dstack([gray, sobelx, sobely]).astype(np.uint8)

【讨论】:

  • 没有给予足够的关注。现已修复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-12
  • 2019-01-17
  • 1970-01-01
  • 2014-03-17
相关资源
最近更新 更多