search
HomeBackend DevelopmentPython TutorialHow to optimize processing of high-resolution images in Python to find precise white circular areas?

How to optimize processing of high-resolution images in Python to find precise white circular areas?

Python efficiently process high-resolution images and accurately locate white circular areas

This article explores how to efficiently process high-resolution images of 9000x7000 pixels using Python and OpenCV to accurately find two white circular areas. The original code has missed and misdetected problems. The following provides optimization solutions.

Problem description

Objective: Accurately locate two white circular areas in a high-resolution image. The existing code uses the Hough circle transformation, but the result is not ideal and there are a lot of misjudgments.

Optimization strategy

In order to improve detection accuracy, images need to be preprocessed and a more robust detection method is adopted. The following steps are gradually optimized:

  1. Image Preprocessing: High-resolution image processing is time-consuming and therefore requires optimization. First of all, when reading an image, you can consider reducing the image size and reducing the calculation complexity, but you need to pay attention to the balance between the size reduction ratio and accuracy. You can use the cv2.resize function and select the appropriate interpolation method (e.g. cv2.INTER_AREA for shrinking).

  2. Enhanced contrast: To highlight the white circular area, image contrast can be enhanced. You can use histogram equalization ( cv2.equalizeHist ) or CLAHE (Contrast Limited Adaptive Histogram Equalization, cv2.createCLAHE ). CLAHE can better handle local contrast differences.

  3. Threshold segmentation: After converting the image to a grayscale graph, use adaptive threshold segmentation ( cv2.adaptiveThreshold ) instead of a simple global threshold segmentation. Adaptive threshold segmentation can better adapt to the brightness changes in different areas of the image. A suitable adaptive method (e.g. cv2.ADAPTIVE_THRESH_GAUSSIAN_C ) and block size can be selected.

  4. Morphological operation: Use morphological opening operations ( cv2.morphologyEx , cv2.MORPH_OPEN ) to remove noise and fine impurities in the image to make the circular area clearer. You need to choose the appropriate structural element size.

  5. Contour detection and filtering: Use cv2.findContours function to detect image contours. When filtering outlines, interference items can be eliminated based on features such as the contour area, circumference, and circularity, and only contours that conform to the white circular characteristics are retained. The circularity can be calculated using the contour area and perimeter.

  6. Minimum circumference: For the filtered contour, you can use cv2.minEnclosingCircle function to fit the minimum circumference to obtain the center coordinates and radius.

Improved code framework (the parameters need to be adjusted according to the actual image):

 import cv2
import numpy as np

image_path = r"C:\Users\17607\Desktop\smls pictures\Pic_20231122151507973.bmp"

img = cv2.imread(image_path)
img_resized = cv2.resize(img, (img.shape[1] // 4, img.shape[0] // 4), interpolation=cv2.INTER_AREA) #Resize, for example to shrink to 1/4

gray = cv2.cvtColor(img_resized, cv2.COLOR_BGR2GRAY)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
gray = clahe.apply(gray)

thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

kernel = np.ones((5,5), np.uint8)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

contours, _ = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

circles = []
for cnt in contours:
    area = cv2.contourArea(cnt)
    perimeter = cv2.arcLength(cnt, True)
    if perimeter > 0: #Avoid zero-deletion errorscircularity = 4 * np.pi * area / (perimeter ** 2)
        if area > 100 and circuitry > 0.7: #Adjust the threshold (x,y) according to the actual situation, radius = cv2.minEnclosingCircle(cnt)
            circles.append(((int(x), int(y)), int(radius)))

# Draw the result (remember to adjust the coordinates and radius back to the original image according to the scaling ratio)
for (x,y),radius in circles:
    cv2.circle(img, (x*4, y*4), radius*4, (0,255,0), 2) # The scale is 4, remember to modify cv2.imshow('Detected Circles', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Note: Parameters in the code (such as thresholds, kernel size, area, and circularity thresholds for morphological operations) need to be adjusted according to the actual image to obtain the best results. It is recommended to gradually adjust the parameters and observe the results. In addition, consider adding an exception handling mechanism, such as handling the situation where image reading fails. Finally, remember to adjust the coordinates and radius of the detection result back to the original image according to the scaling ratio.

The above is the detailed content of How to optimize processing of high-resolution images in Python to find precise white circular areas?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools