


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:
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).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.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.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.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.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!

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools