简体   繁体   English

使用OpenCV自动进行红色阈值化

[英]Automatic Red colour thresholding using OpenCV

Heres my input image: 这是我的输入图片:

在此处输入图片说明

I am plotting histogram of this image using the following code: 我正在使用以下代码绘制此图像的直方图:

import cv2 import numpy as np from matplotlib import pyplot as plt 从matplotlib导入cv2导入numpy作为np从ptp导入pyplot

img = cv2.imread('red.jpg')
color = ('b','g','r')
for i,col in enumerate(color):
    histr = cv2.calcHist([img],[i],None,[256],[0,256])
    plt.plot(histr,color = col)
    plt.xlim([0,256])
plt.show()

Here is the plotted histogram output: On the left hand side is the original histogram and on the right hand side is the zoomed version: 这是绘制的直方图输出:左侧是原始直方图,右侧是缩放版本:

在此处输入图片说明

My starting point is 255 and ending point is zero. 我的起点是255,终点是零。

All my important data lies on the range of 235 to 255. As at 235 the line becomes straight (pl. see right hand side of histogram) 我所有重要的数据都在235到255的范围内。到235时,直线变为直线(请参见直方图的右侧)

I want to write a python - opencv code which finds out when red line of histogram becomes straight and once the number is found after which the line shows minimum deviation delete all the remaining pixels from the image. 我想编写一个python-opencv代码,找出直方图的红线何时变直,一旦找到数字,然后该线显示最小偏差,则从图像中删除所有剩余像素。 In above case delete pixels having value (0 to 235). 在上述情况下,删除具有值(0到235)的像素。 How can this be achieved ? 如何实现呢?

Histogram is basically arrays (bins). 直方图基本上是数组(bin)。 The opencv histogram bins you create, you can check for the number of values & mean values in each bin, and compare it with previous bin. 您创建的opencv直方图垃圾箱,您可以检查每个垃圾箱中的值和平均值的数量,并将其与以前的垃圾箱进行比较。 (More like a sliding window). (更像是滑动窗口)。 If you find the difference to be greater than a threshold, then consider them to be chosen bins(pixels). 如果发现差异大于阈值,则将其视为选择的bin(像素)。

This is a technique used to identify peaks in a 1D array. 这是用于识别一维阵列中的峰的技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM