init - 初始化项目
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
from __future__ import print_function
|
||||
from __future__ import division
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
import argparse
|
||||
|
||||
def Hist_and_Backproj(val):
|
||||
## [initialize]
|
||||
bins = val
|
||||
histSize = max(bins, 2)
|
||||
ranges = [0, 180] # hue_range
|
||||
## [initialize]
|
||||
|
||||
## [Get the Histogram and normalize it]
|
||||
hist = cv.calcHist([hue], [0], None, [histSize], ranges, accumulate=False)
|
||||
cv.normalize(hist, hist, alpha=0, beta=255, norm_type=cv.NORM_MINMAX)
|
||||
## [Get the Histogram and normalize it]
|
||||
|
||||
## [Get Backprojection]
|
||||
backproj = cv.calcBackProject([hue], [0], hist, ranges, scale=1)
|
||||
## [Get Backprojection]
|
||||
|
||||
## [Draw the backproj]
|
||||
cv.imshow('BackProj', backproj)
|
||||
## [Draw the backproj]
|
||||
|
||||
## [Draw the histogram]
|
||||
w = 400
|
||||
h = 400
|
||||
bin_w = int(round(w / histSize))
|
||||
histImg = np.zeros((h, w, 3), dtype=np.uint8)
|
||||
|
||||
for i in range(bins):
|
||||
cv.rectangle(histImg, (i*bin_w, h), ( (i+1)*bin_w, h - int(np.round( hist[i]*h/255.0 )) ), (0, 0, 255), cv.FILLED)
|
||||
|
||||
cv.imshow('Histogram', histImg)
|
||||
## [Draw the histogram]
|
||||
|
||||
## [Read the image]
|
||||
parser = argparse.ArgumentParser(description='Code for Back Projection tutorial.')
|
||||
parser.add_argument('--input', help='Path to input image.', default='home.jpg')
|
||||
args = parser.parse_args()
|
||||
|
||||
src = cv.imread(cv.samples.findFile(args.input))
|
||||
if src is None:
|
||||
print('Could not open or find the image:', args.input)
|
||||
exit(0)
|
||||
## [Read the image]
|
||||
|
||||
## [Transform it to HSV]
|
||||
hsv = cv.cvtColor(src, cv.COLOR_BGR2HSV)
|
||||
## [Transform it to HSV]
|
||||
|
||||
## [Use only the Hue value]
|
||||
ch = (0, 0)
|
||||
hue = np.empty(hsv.shape, hsv.dtype)
|
||||
cv.mixChannels([hsv], [hue], ch)
|
||||
## [Use only the Hue value]
|
||||
|
||||
## [Create Trackbar to enter the number of bins]
|
||||
window_image = 'Source image'
|
||||
cv.namedWindow(window_image)
|
||||
bins = 25
|
||||
cv.createTrackbar('* Hue bins: ', window_image, bins, 180, Hist_and_Backproj )
|
||||
Hist_and_Backproj(bins)
|
||||
## [Create Trackbar to enter the number of bins]
|
||||
|
||||
## [Show the image]
|
||||
cv.imshow(window_image, src)
|
||||
cv.waitKey()
|
||||
## [Show the image]
|
||||
@@ -0,0 +1,79 @@
|
||||
from __future__ import print_function
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
import argparse
|
||||
|
||||
low = 20
|
||||
up = 20
|
||||
|
||||
def callback_low(val):
|
||||
global low
|
||||
low = val
|
||||
|
||||
def callback_up(val):
|
||||
global up
|
||||
up = val
|
||||
|
||||
def pickPoint(event, x, y, flags, param):
|
||||
if event != cv.EVENT_LBUTTONDOWN:
|
||||
return
|
||||
|
||||
# Fill and get the mask
|
||||
seed = (x, y)
|
||||
newMaskVal = 255
|
||||
newVal = (120, 120, 120)
|
||||
connectivity = 8
|
||||
flags = connectivity + (newMaskVal << 8 ) + cv.FLOODFILL_FIXED_RANGE + cv.FLOODFILL_MASK_ONLY
|
||||
|
||||
mask2 = np.zeros((src.shape[0] + 2, src.shape[1] + 2), dtype=np.uint8)
|
||||
print('low:', low, 'up:', up)
|
||||
cv.floodFill(src, mask2, seed, newVal, (low, low, low), (up, up, up), flags)
|
||||
mask = mask2[1:-1,1:-1]
|
||||
|
||||
cv.imshow('Mask', mask)
|
||||
Hist_and_Backproj(mask)
|
||||
|
||||
def Hist_and_Backproj(mask):
|
||||
h_bins = 30
|
||||
s_bins = 32
|
||||
histSize = [h_bins, s_bins]
|
||||
h_range = [0, 180]
|
||||
s_range = [0, 256]
|
||||
ranges = h_range + s_range # Concat list
|
||||
channels = [0, 1]
|
||||
|
||||
# Get the Histogram and normalize it
|
||||
hist = cv.calcHist([hsv], channels, mask, histSize, ranges, accumulate=False)
|
||||
cv.normalize(hist, hist, alpha=0, beta=255, norm_type=cv.NORM_MINMAX)
|
||||
|
||||
# Get Backprojection
|
||||
backproj = cv.calcBackProject([hsv], channels, hist, ranges, scale=1)
|
||||
|
||||
# Draw the backproj
|
||||
cv.imshow('BackProj', backproj)
|
||||
|
||||
# Read the image
|
||||
parser = argparse.ArgumentParser(description='Code for Back Projection tutorial.')
|
||||
parser.add_argument('--input', help='Path to input image.', default='home.jpg')
|
||||
args = parser.parse_args()
|
||||
|
||||
src = cv.imread(cv.samples.findFile(args.input))
|
||||
if src is None:
|
||||
print('Could not open or find the image:', args.input)
|
||||
exit(0)
|
||||
|
||||
# Transform it to HSV
|
||||
hsv = cv.cvtColor(src, cv.COLOR_BGR2HSV)
|
||||
|
||||
# Show the image
|
||||
window_image = 'Source image'
|
||||
cv.namedWindow(window_image)
|
||||
cv.imshow(window_image, src)
|
||||
|
||||
# Set Trackbars for floodfill thresholds
|
||||
cv.createTrackbar('Low thresh', window_image, low, 255, callback_low)
|
||||
cv.createTrackbar('High thresh', window_image, up, 255, callback_up)
|
||||
# Set a Mouse Callback
|
||||
cv.setMouseCallback(window_image, pickPoint)
|
||||
|
||||
cv.waitKey()
|
||||
Reference in New Issue
Block a user