AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 78968073
Accepted
Pubg Mobile
Pubg Mobile
Asked: 2024-09-10 13:57:21 +0800 CST2024-09-10 13:57:21 +0800 CST 2024-09-10 13:57:21 +0800 CST

仅检测图像中最左侧的框

  • 772

我有一张包含手机品牌名称的 JPG 图像:
在此处输入图片描述

现在我想通过 python 脚本检测每个单词的第一个字符,
为此我编写了以下 python 脚本:

import cv2
import numpy as np
from tkinter import Tk, Canvas, Frame, Scrollbar, BOTH, VERTICAL, HORIZONTAL
from PIL import Image, ImageTk

# Function to draw rectangles around shapes and display using Tkinter
def draw_rectangles(image_path):
    # Create a Tkinter window to display the image
    root = Tk()
    root.title("Image with Left-Most Rectangles Only")

    # Load the image
    image = cv2.imread(image_path)
    # Convert the image to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Apply adaptive thresholding to get better separation of text
    thresh = cv2.adaptiveThreshold(
        gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2
    )

    # Find contours in the binary image
    contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # Dictionary to store contours grouped by Y-coordinate ranges
    contours_by_y = {}

    # Sort contours by X-coordinate to ensure we pick the left-most character first
    sorted_contours = sorted(contours, key=lambda c: cv2.boundingRect(c)[0])

    # Group contours by their Y coordinate to keep only the left-most rectangle per Y range
    for contour in sorted_contours:
        x, y, w, h = cv2.boundingRect(contour)
        if w > 15 and h > 15:  # Adjust the size filter to remove small artifacts
            aspect_ratio = w / float(h)
            # Ensure the aspect ratio is within the typical range of letters
            if 0.2 < aspect_ratio < 5:
                y_range = y // 20  # Group by a smaller Y coordinate range for better separation

                # Check if the current rectangle is more left-most in X within its Y range
                if y_range not in contours_by_y:
                    contours_by_y[y_range] = (x, y, w, h)  # Store the first contour found in this range
                else:
                    # Compare and keep the left-most (smallest X) rectangle
                    current_x, _, _, _ = contours_by_y[y_range]
                    # Check distance between new contour and the existing one to avoid close detection
                    if x < current_x and (x - current_x) > 20:  # Distance threshold to filter out close contours
                        contours_by_y[y_range] = (x, y, w, h)

    # Draw only the left-most rectangles
    for (x, y, w, h) in contours_by_y.values():
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)  # Red color in BGR

    # Convert the image to RGB (OpenCV uses BGR by default)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    # Convert the image to a format Tkinter can use
    image_pil = Image.fromarray(image_rgb)
    image_tk = ImageTk.PhotoImage(image_pil)

    # Create a frame for the Canvas and scrollbars
    frame = Frame(root)
    frame.pack(fill=BOTH, expand=True)

    # Create a Canvas widget to display the image
    canvas = Canvas(frame, width=image_tk.width(), height=image_tk.height())
    canvas.pack(side="left", fill="both", expand=True)

    # Add scrollbars to the Canvas
    v_scrollbar = Scrollbar(frame, orient=VERTICAL, command=canvas.yview)
    v_scrollbar.pack(side="right", fill="y")

    h_scrollbar = Scrollbar(frame, orient=HORIZONTAL, command=canvas.xview)
    h_scrollbar.pack(side="bottom", fill="x")

    canvas.configure(yscrollcommand=v_scrollbar.set, xscrollcommand=h_scrollbar.set)
    canvas.create_image(0, 0, anchor="nw", image=image_tk)
    canvas.config(scrollregion=canvas.bbox("all"))

    # Keep a reference to the image to prevent garbage collection
    canvas.image = image_tk

    root.mainloop()

# Path to your image
image_path = r"E:\Desktop\mobile_brands\ORG_027081-Recovered.jpg"

# Call the function
draw_rectangles(image_path)

但我不知道为什么它效果不好。这个脚本的准确率是 90%。例如在上图中,它检测到“Samsung”中的“a”字符
在此处输入图片描述

我的脚本问题在哪里?
我该如何解决这个问题?
也许通过 Y 和 X 坐标无法检测图像中最左边的框。
请注意,我不想使用 OCR

python
  • 1 1 个回答
  • 58 Views

1 个回答

  • Voted
  1. Best Answer
    acw1668
    2024-09-10T15:40:49+08:002024-09-10T15:40:49+08:00

    我将首先按 y 坐标对这些轮廓进行排序,然后按行对排序后的轮廓进行分组。最后在每行中绘制最左边的轮廓:

    # Sort contours by Y-coordinate
    sorted_contours = sorted(contours, key=lambda c: cv2.boundingRect(c)[1])
    
    # group contours by row
    last_y = None
    rows = []
    for contour in sorted_contours:
        x, y, w, h = cv2.boundingRect(contour)
        if w > 15 or h > 15:
            if last_y is None or abs(last_y-y) > 50:  # 50 is for provided image, it may not work for other image
                # this is a new row
                rows.append([])
                last_y = y  # save the reference y-coordinate
            rows[-1].append((x, y, w, h)) # append item into current row
    
    for row in rows:
        # show left-most one in current row
        x, y, w, h = sorted(row, key=lambda x: x[0])[0]
        cv2.rectangle(image, (x, y), (x+w, y+h), (0, 0, 255), 2)
    

    请注意,我假设行之间的 y 坐标差异大于 50 像素。

    结果:

    在此处输入图片描述

    • 1

相关问题

  • 如何将 for 循环拆分为 3 个单独的数据框?

  • 如何检查 Pandas DataFrame 中的所有浮点列是否近似相等或接近

  • “load_dataset”如何工作,因为它没有检测示例文件?

  • 为什么 pandas.eval() 字符串比较返回 False

  • Python tkinter/ ttkboostrap dateentry 在只读状态下不起作用

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve