Question

In: Computer Science

I'm having trouble creating a histogram using openCV (color segmentation)

I'm having trouble creating a histogram using openCV (color segmentation)

Solutions

Expert Solution

Code:

from __future__ import print_function
from __future__ import division
import cv2 as cv
import numpy as np

## [Load image]
src = cv.imread(cv.samples.findFile('img.jpg'))
if src is None:
    print('Could not open or find the image')
    exit(0)

## [Separate the image in 3 places ( B, G and R )]
bgr_planes = cv.split(src)

## [Establish the number of bins]
histSize = 256

## [Set the ranges ( for B,G,R) )]
histRange = (0, 256) # the upper boundary is exclusive

## [Compute the histograms]
b_hist = cv.calcHist(bgr_planes, [0], None, [histSize], histRange, accumulate=False)
g_hist = cv.calcHist(bgr_planes, [1], None, [histSize], histRange, accumulate=False)
r_hist = cv.calcHist(bgr_planes, [2], None, [histSize], histRange, accumulate=False)

## [Draw the histograms for B, G and R]
hist_w = 512
hist_h = 400
bin_w = int(round( hist_w/histSize ))

histImage = np.zeros((hist_h, hist_w, 3), dtype=np.uint8)
## [Draw the histograms for B, G and R]

## [Normalize the result to ( 0, histImage.rows )]
cv.normalize(b_hist, b_hist, alpha=0, beta=hist_h, norm_type=cv.NORM_MINMAX)
cv.normalize(g_hist, g_hist, alpha=0, beta=hist_h, norm_type=cv.NORM_MINMAX)
cv.normalize(r_hist, r_hist, alpha=0, beta=hist_h, norm_type=cv.NORM_MINMAX)

    
## [Draw for each channel]
for i in range(1, histSize):
    cv.line(histImage, ( bin_w*(i-1), hist_h - round(int(b_hist[i-1])) ),
            ( bin_w*(i), hist_h - round(int(b_hist[i])) ),
            ( 255, 0, 0), thickness=2)
    cv.line(histImage, ( bin_w*(i-1), hist_h - round(int(g_hist[i-1])) ),
            ( bin_w*(i), hist_h - round(int(g_hist[i])) ),
            ( 0, 255, 0), thickness=2)
    cv.line(histImage, ( bin_w*(i-1), hist_h - round(int(r_hist[i-1])) ),
            ( bin_w*(i), hist_h - round(int(r_hist[i])) ),
            ( 0, 0, 255), thickness=2)

## [Display]
cv.imshow("Source image", src );
cv.imshow('calcHist Demo', histImage)
cv.waitKey()

Code Snippet:

Original Image:

Histogram:


Related Solutions

Using the Issue, Rule, Analysis, Conclusion outline, I'm having trouble creating the IRAC for this situation....Happy...
Using the Issue, Rule, Analysis, Conclusion outline, I'm having trouble creating the IRAC for this situation....Happy City opened bidding for an airport construction project, by the usual process of advertising a request for bids. Crafty Construction submitted the lowest responsive bid. When the Happy City Council met to review the bid the Council members discovered the bid exceeded the budget for the project and discussed the possibility of negotiating a bid reduction. The city manager told the Council that Chris...
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and...
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and returns the number of times the symbol 'a' occurs in it. Note: do not count a's that might occur in a sublist within the list.
Using dev c++ I'm having trouble with classes. I think the part that I am not...
Using dev c++ I'm having trouble with classes. I think the part that I am not understanding is sending data between files and also using bool data. I've been working on this program for a long time with many errors but now I've thrown in my hat to ask for outside help. Here is the homework that has given me so many issues: The [REDACTED] Phone Store needs a program to compute phone charges for some phones sold in the...
I'm having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
I'm having trouble programming connect four board game using linked lists, sets and maps in c++....
I'm having trouble programming connect four board game using linked lists, sets and maps in c++. Can you code connect four game using these concepts.
We are learning about Ramayana in Mythology and I'm having a bit of trouble understanding the...
We are learning about Ramayana in Mythology and I'm having a bit of trouble understanding the story. Why would Rama be set apart as a true hero? The story of Rama and Sita is a favorite story that parents tell their children. What purpose does the Ramayana serve as an instructional story for Indian culture? What effect do the test and temptations have on the heroic character?
I'm having trouble with these few questions out of a long list of journal entries that...
I'm having trouble with these few questions out of a long list of journal entries that I have to record for a project. If you could please state the journal entries for these and why that would be very helpful. Thank you! May 2 – Sold merchandise on credit to Yellow Rock Company, Invoice No. 9501, for $4,500 (cost is $2,000). I get the first part of the journal entry but don't know how to record the cost. May 3...
Balance in neutral solution: MnO4- + S2O32- ---> SO42- + MnO2 I'm specifically having trouble with...
Balance in neutral solution: MnO4- + S2O32- ---> SO42- + MnO2 I'm specifically having trouble with using H2O, H+, and OH- in balancing the half reactions.
I'm having trouble with knowing what type of dye you need to use for the microorganisms...
I'm having trouble with knowing what type of dye you need to use for the microorganisms in Microbiology, can you please explain
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT