File size: 3,356 Bytes
56fc8d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import cv2
import numpy as np
import os
import matplotlib.pyplot as plt
import io
from PIL import Image
import numpy as np

def process_bgr_image(img, lb, ub, channel):

    # Define the lower and upperbounds based on the image channel.
    if channel == 'Blue':
        lb = np.array([lb, 0, 0], np.uint8)
        ub = np.array([ub, 255, 255], np.uint8)
    elif channel == 'Green':
        lb = np.array([0, lb, 0], np.uint8)
        ub = np.array([255, ub, 255], np.uint8)
    elif channel == 'Red':
        lb = np.array([0, 0, lb], np.uint8)
        ub = np.array([255, 255, ub], np.uint8)

    # Calculate the histograms for the BGR color space and the Hue channel in the HSV image.
    hist_b, hist_g, hist_r = calculateHistogram(img, mode='BGR')

    # Plot the histograms
    hist_bgr = plotHistogram((hist_b, hist_g, hist_r), mode='BGR')

    # Calculate the mask for the given bounds from hsv image
    op = calculateMask(img, lb, ub, mode='BGR')

    return  hist_bgr, op

def process_hsv_image(img, lb, ub):
    
    # Conver the image to the HSV color space
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    # Define the lower and upperbounds for Hue
    lb = np.array([lb, 0, 0], np.uint8)
    ub = np.array([ub, 255, 255], np.uint8)

    # Calculate the histograms for the BGR color space and the Hue channel in the HSV image.
    hist_hue = calculateHistogram(img_hsv, mode='HSV')

    # Plot the histogram.
    hist_hsv = plotHistogram(hist_hue, mode='HSV')

    # Calculate the mask for the given bounds from hsv image
    op = calculateMask((img_hsv, img), lb, ub, mode='HSV')

    return hist_hsv, op


def calculateHistogram(img, mode):

    if mode == 'BGR':
        hist_b = cv2.calcHist([img], [0], None, [256], [0,255])
        hist_g = cv2.calcHist([img], [1], None, [256], [0,255])
        hist_r = cv2.calcHist([img], [2], None, [256], [0,255])

        return hist_b, hist_g, hist_r
    
    elif mode == 'HSV':

        hist_hue = cv2.calcHist([img], [0], None, [180], [0,180])
        return hist_hue

def plotHistogram(hists, mode):

    if mode == 'BGR':
        hist_b, hist_g, hist_r = hists
        plt.figure(figsize=(10,2))
        plt.plot(hist_b, color='b', label='Blue')
        plt.plot(hist_g, color='g', label='Green')
        plt.plot(hist_r, color='r', label='Red')
        plt.xlim([0, 255])
        plt.legend()
        buf_bgr= io.BytesIO()
        plt.savefig(buf_bgr, format='png')
        buf_bgr.seek(0)
        hist_bgr = Image.open(buf_bgr)
        plt.close()
        return hist_bgr
    
    if mode == 'HSV':

        hist_hue = hists
        plt.figure(figsize=(10,2))
        plt.plot(hist_hue, color='black', label='Black')
        plt.xlim([0, 180])
        buf_hsv = io.BytesIO()
        plt.savefig(buf_hsv, format='png')
        buf_hsv.seek(0)
        hist_hsv = Image.open(buf_hsv)
        plt.close()
        return hist_hsv

def calculateMask(imgs, lb, ub, mode):

    op = None

    if mode == 'BGR':
        img = imgs
        mask_1d = cv2.inRange(img, lb, ub)
        mask_3d = cv2.merge([mask_1d, mask_1d, mask_1d])
        op = cv2.bitwise_and(img, mask_3d)
    
    if mode == 'HSV':
        img_hsv, img = imgs
        mask_1d = cv2.inRange(img_hsv, lb, ub)
        mask_3d = cv2.merge([mask_1d, mask_1d, mask_1d])
        op = cv2.bitwise_and(img, mask_3d)

    return op