在图像中识别可见的石头 - opencv&深度学习

发布于 2025-01-23 21:15:25 字数 1747 浏览 0 评论 0原文

我有图像中存在的石头的样品图像。我只需要识别可见的石头。我尝试的方法是基于阈值的过滤和检测Cv2.Contours 。另外,我正在研究 enet体系结构 。样品图像在下面。
示例Image1: 示例Image2:

我尝试用于基于轮廓的检测的代码如

image = cv2.imread(os.path.join(img_path, img_name2))
# threshold based customization
lower_bound = np.array([0, 0, 0])
upper_bound = np.array([250,55,100])

hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
#masking the image using inRange() function
imagemask = cv2.inRange(hsv, lower_bound, upper_bound)
plt.figure(figsize=(20,10)) 
plt.imshow(imagemask, cmap="gray")
# erode and diluation to smoothen the edeges
final_mask = cv2.erode(imagemask, np.ones((3, 3), dtype=np.uint8))
final_mask = cv2.dilate(imagemask, np.ones((5, 5), dtype=np.uint8))
# find contours based on the mask
contours = cv2.findContours(final_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# draw contours
img_conts = cv2.drawContours(image.copy(), contours[0], -1, (0,255,0), 3)

plt.figure(figsize=(20,10)) 
plt.imshow(img_conts, cmap="gray")

示例轮廓OUPUT下方。我知道可以在此处调整阈值以获得更好的结果。

但是,我在这里寻找的任何更好的方法或解决方案都可以在这个繁重的环境中起作用,以检测像石头这样的小颗粒。有什么想法可以更好地解决吗?

I have samples images of stones present in the images. I need to identify the visible stones only. The approach which I tried is threshold based filtering and detecting cv2.contours. Also, I am looking into ENet Architecture for semantic segmentation based deep learning approach. The samples images are below.
Example image1:
enter image description here
Example image2:
enter image description here

The code which I tried for contour based detection is as below

image = cv2.imread(os.path.join(img_path, img_name2))
# threshold based customization
lower_bound = np.array([0, 0, 0])
upper_bound = np.array([250,55,100])

hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
#masking the image using inRange() function
imagemask = cv2.inRange(hsv, lower_bound, upper_bound)
plt.figure(figsize=(20,10)) 
plt.imshow(imagemask, cmap="gray")
# erode and diluation to smoothen the edeges
final_mask = cv2.erode(imagemask, np.ones((3, 3), dtype=np.uint8))
final_mask = cv2.dilate(imagemask, np.ones((5, 5), dtype=np.uint8))
# find contours based on the mask
contours = cv2.findContours(final_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# draw contours
img_conts = cv2.drawContours(image.copy(), contours[0], -1, (0,255,0), 3)

plt.figure(figsize=(20,10)) 
plt.imshow(img_conts, cmap="gray")

The sample contours ouput. I know that the thresholds can be tuned for better results here. enter image description here

But, what I am looking here for the any better approach or solution can work in this heavy environment for detection small particles like stones. Any ideas to solve in better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぶ宁プ宁ぶ 2025-01-30 21:15:25

这是您可以使用Canny Edge检测器来检测图像中的岩石:

import cv2
import numpy as np

def process(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(img_gray, 103, 255, cv2.THRESH_BINARY)
    img_blur = cv2.GaussianBlur(thresh, (23, 23), 0)
    img_canny = cv2.Canny(img_blur, 65, 0)
    img_dilate = cv2.dilate(img_canny, None, iterations=2)
    return cv2.erode(img_dilate, None, iterations=2)

imgs = [cv2.imread("image1.jpg"), cv2.imread("image2.jpg")]

for i, img in enumerate(imgs):
    contours = cv2.findContours(process(img), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[0]
    cv2.drawContours(img, contours, -1, (0, 255, 0), 1)
    cv2.imshow(str(i), img)

cv2.waitKey(0)
cv2.destroyAllWindows()

示例图像1和2的输出:

”在此处输入图像说明“

​使用以下代码的跟踪栏:

import cv2
import numpy as np
from random import randint, sample

def process(img, c_t1, c_t2):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(img_gray, 103, 255, cv2.THRESH_BINARY)
    img_blur = cv2.GaussianBlur(thresh, (23, 23), 0)
    img_canny = cv2.Canny(img_blur, c_t1, c_t2)
    img_dilate = cv2.dilate(img_canny, None, iterations=2)
    return cv2.erode(img_dilate, None, iterations=2)

def show(imgs, win="Image", scale=1):
    imgs = [cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) if len(img.shape) == 2 else img for img in imgs]
    img_concat = np.concatenate(imgs, 1)
    h, w = img_concat.shape[:2]
    cv2.imshow(win, cv2.resize(img_concat, (int(w * scale), int(h * scale))))

d = {"Canny Threshold 1": (65, 500),
     "Canny Threshold 2": (0, 500)}

imgs = [cv2.imread("image1.jpg"), cv2.imread("image2.jpg")]

cv2.namedWindow("Track Bars")
for i in d:
    cv2.createTrackbar(i, "Track Bars", *d[i], id)

while True:
    c_t1, c_t2 = (cv2.getTrackbarPos(i, "Track Bars") for i in d)
    for i, img in enumerate(imgs):
        img_copy = img.copy()
        processed = process(img, c_t1, c_t2)
        contours = cv2.findContours(processed, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[0]
        cv2.drawContours(img_copy, contours, -1, (0, 255, 0), 1)
        show([img_copy, processed], str(i))
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

cv2.destroyAllWindows()

outupt:

”在此处输入图像说明”
(单击图像以展开)

Here is how you can use the Canny edge detector to detect the rocks in your images:

import cv2
import numpy as np

def process(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(img_gray, 103, 255, cv2.THRESH_BINARY)
    img_blur = cv2.GaussianBlur(thresh, (23, 23), 0)
    img_canny = cv2.Canny(img_blur, 65, 0)
    img_dilate = cv2.dilate(img_canny, None, iterations=2)
    return cv2.erode(img_dilate, None, iterations=2)

imgs = [cv2.imread("image1.jpg"), cv2.imread("image2.jpg")]

for i, img in enumerate(imgs):
    contours = cv2.findContours(process(img), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[0]
    cv2.drawContours(img, contours, -1, (0, 255, 0), 1)
    cv2.imshow(str(i), img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output for sample images 1 and 2:

enter image description here

enter image description here

You can also tweak the parameters using OpenCV trackbars using the code below:

import cv2
import numpy as np
from random import randint, sample

def process(img, c_t1, c_t2):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(img_gray, 103, 255, cv2.THRESH_BINARY)
    img_blur = cv2.GaussianBlur(thresh, (23, 23), 0)
    img_canny = cv2.Canny(img_blur, c_t1, c_t2)
    img_dilate = cv2.dilate(img_canny, None, iterations=2)
    return cv2.erode(img_dilate, None, iterations=2)

def show(imgs, win="Image", scale=1):
    imgs = [cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) if len(img.shape) == 2 else img for img in imgs]
    img_concat = np.concatenate(imgs, 1)
    h, w = img_concat.shape[:2]
    cv2.imshow(win, cv2.resize(img_concat, (int(w * scale), int(h * scale))))

d = {"Canny Threshold 1": (65, 500),
     "Canny Threshold 2": (0, 500)}

imgs = [cv2.imread("image1.jpg"), cv2.imread("image2.jpg")]

cv2.namedWindow("Track Bars")
for i in d:
    cv2.createTrackbar(i, "Track Bars", *d[i], id)

while True:
    c_t1, c_t2 = (cv2.getTrackbarPos(i, "Track Bars") for i in d)
    for i, img in enumerate(imgs):
        img_copy = img.copy()
        processed = process(img, c_t1, c_t2)
        contours = cv2.findContours(processed, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[0]
        cv2.drawContours(img_copy, contours, -1, (0, 255, 0), 1)
        show([img_copy, processed], str(i))
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

cv2.destroyAllWindows()

Output:

enter image description here
(Click image to expand)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文