OpenCV – 从图像中移除彩色区域

huangapple go评论91阅读模式
英文:

Open CV - Removing a colored region from an image

问题

我正在尝试使用OpenCV和Java从图像中移除特定颜色(在这种情况下是灰色)。
我希望图像的其他部分都存在于输出图像中。

这是我尝试过的代码。

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat original = Imgcodecs.imread("C:\\ABC\\App2.jpg");

Mat dst = new Mat();
Core.inRange(original, new Scalar(88, 88, 88), new Scalar(88, 88, 88), dst);
Imgcodecs.imwrite("C:\\ABC\\mask4.jpg", dst);

原始图像:https://freeimage.host/i/dkdqXe
原始输出:https://freeimage.host/i/dkdukB

它的作用是,图像中灰色部分显示为白色,而图像的其余部分都是黑色。但我只想从原始图像中移除(或使其透明)灰色部分,而不影响图像的其他部分。非常感谢您提前的任何帮助或输入。

英文:

I am trying to remove a particular color( Grey in this case ) from an image using OpenCV & Java.
I want all the other parts of the image should be present in the output image

Here is what I have tried.

     System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
     Mat original = Imgcodecs.imread("C:\\ABC\\App2.jpg");
     
     Mat dst = new Mat();
     Core.inRange(original, new Scalar( 88, 88, 88), new Scalar( 88, 88, 88), dst);
     Imgcodecs.imwrite("C:\\ABC\\mask4.jpg", dst);

Original Image: https://freeimage.host/i/dkdqXe
Original Output: https://freeimage.host/i/dkdukB

What it does is, the grey colored portion of the image is shown in white and the rest of the image is all black. But I just want to remove ( or make transparent ) the grey colored portion from the original image without affecting the other parts of the image. Any help pr input is much appreciated. Thanks in advance.

答案1

得分: 0

在Python中,您通过提供点的范围来定义区域:

import cv2
img = cv2.imread('test.jpg', -1)
crop_img = img[y:y+h, x:x+w]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)

希望这能帮助您在Java中解决相应的问题。

更新 #2

import cv2
img = cv2.imread('lena.jpg', -1)
roi = cv2.selectROI(img, False)
crop_img = img[roi[1]:(roi[1]+roi[3]), roi[0]:(roi[0]+roi[2])]
cv2.imwrite('cropped', crop_img)
英文:

in Python you define the area by giving the range of the points:

import cv2
img=cv2.imread('test.jpg', -1)
crop_img = img[y:y+h, x:x+w]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)

Hope that can help you figuring in out in Java respectivly.

UPDATE #2

import cv2
img = cv2.imread('lena.jpg', -1)
roi = cv2.selectROI(img, False)
crop_img = img[roi[1]:(roi[1]+roi[3]), roi[0]:(roi[0]+roi[2])]
cv2.imwrite('cropped',crop_img)

huangapple
  • 本文由 发表于 2020年8月20日 18:58:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63503609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定