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...
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 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
If anyone could simplify this for me. I'm having trouble understanding the material and I just...
If anyone could simplify this for me. I'm having trouble understanding the material and I just need a keep it simple stupid approach Discuss the various non influential as well as influential investments that company may have on their financial statements. Also compare and contrast how they are treated/recorded on the companies financial statements.
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator...
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator is 0 it throws the ZeroDenominatorException and the catch catches to guarantee that the denominator is never 0. /** * The main class is the driver for my Rational project. */ public class Main { /** * Main method is the entry point to my code. * @param args The command line arguments. */ public static void main(String[] args) { int numerator, denominator =...
​​​​​​This is an assignment that I'm having trouble figuring out in python: Modify Node class so...
​​​​​​This is an assignment that I'm having trouble figuring out in python: Modify Node class so it has a field called frequency and initialize it as one. Add a search function. If a number is in the list, return its frequency. Modify Insert function. Remove push, append, and insertAfter as one function called insert(self, data). If you insert a data that is already in the list, simply increase this node’s frequency. If the number is not in the list, add...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT