OpenCV Project Archives - Indian AI Production https://indianaiproduction.com/opencv-project/ Artificial Intelligence Education Free for Everyone Thu, 30 Sep 2021 03:58:45 +0000 en-US hourly 1 https://i0.wp.com/indianaiproduction.com/wp-content/uploads/2019/06/Channel-logo-in-circle-473-x-472-px.png?fit=32%2C32&ssl=1 OpenCV Project Archives - Indian AI Production https://indianaiproduction.com/opencv-project/ 32 32 163118462 How to Change Image File Extension https://indianaiproduction.com/change-image-extension-opencv-python/ https://indianaiproduction.com/change-image-extension-opencv-python/#respond Thu, 30 Sep 2021 03:58:42 +0000 https://indianaiproduction.com/?p=1836 In Python OpenCV Tutorial, we are going to change the image file extension from one form to another using  cv2.imwrite() function. Like JPG to PNG or PNG to JPG. Change Image File Format Supported Image file format by OpenCV Opencv Support Below Image file format to read & write(Save): Windows bitmaps – *.bmp, *.dib (always …

How to Change Image File Extension Read More »

The post How to Change Image File Extension appeared first on Indian AI Production.

]]>
In Python OpenCV Tutorial, we are going to change the image file extension from one form to another using  cv2.imwrite() function. Like JPG to PNG or PNG to JPG.

Change Image File Format

Supported Image file format by OpenCV

Opencv Support Below Image file format to read & write(Save):

Windows bitmaps – *.bmp, *.dib (always supported)
JPEG files – *.jpeg, *.jpg, *.jpe (see the Notes section)
JPEG 2000 files – *.jp2 (see the Notes section)
Portable Network Graphics – *.png (see the Notes section)
Portable image format – *.pbm, *.pgm, *.ppm (always supported)
Sun rasters – *.sr, *.ras (always supported)
TIFF files – *.tiff, *.tif (see the Notes section)

Read Image

# Import Libraries
import cv2

## Read Image
#img_path = r"data/pexels-bess-hamiti-35188.jpg"
img_path = r"data/girl_face.png"
img = cv2.imread(img_path)
#img = cv2.resize(img, (1280,720))

cv2.imshow("Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Change Image extension JPG to PNG

img_output_path = r"data/output/girl_boy.png"
responce = cv2.imwrite(img_output_path, img)

if responce:
    print("image stored at location: ",img_output_path)

Change Image extension PNG to JPG

img_output_path = r"data/output/girl_model.jpg"
responce = cv2.imwrite(img_output_path, img)

if responce:
    print("image stored at location: ",img_output_path)

Change Image Extension from any format to another Format

img_output_path = r"data/output/girl_model_tiff.tiff"
responce = cv2.imwrite(img_output_path, img)

if responce:
    print("image stored at location: ",img_output_path)

The post How to Change Image File Extension appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/change-image-extension-opencv-python/feed/ 0 1836
Change the Pixel Value of an Image in OpenCV Python | OpenCV Tutorial https://indianaiproduction.com/change-image-pixels-opencv-python/ https://indianaiproduction.com/change-image-pixels-opencv-python/#respond Sat, 14 Aug 2021 05:32:21 +0000 https://indianaiproduction.com/?p=1831 In Python OpenCV Tutorial, Explained How to Change Pixels Color in an image using NumPy Slicing and indexing. Get the answers of below questions: What is pixel? What is resolution? How to read image pixel? How to get image pixel? How to print image pixel? How to change the pixel value of an image in python …

Change the Pixel Value of an Image in OpenCV Python | OpenCV Tutorial Read More »

The post Change the Pixel Value of an Image in OpenCV Python | OpenCV Tutorial appeared first on Indian AI Production.

]]>
In Python OpenCV Tutorial, Explained How to Change Pixels Color in an image using NumPy Slicing and indexing.

Get the answers of below questions:

  1. What is pixel?
  2. What is resolution?
  3. How to read image pixel?
  4. How to get image pixel?
  5. How to print image pixel?
  6. How to change the pixel value of an image in python opencv?
  7. How do I pixelate an image in OpenCV?
  8. How do I change the pixel value of an image in OpenCV Python?

Add colour pixels in Image

Impor Libraries

"""
Video Tutorial Link:
Change the Pixel Value of an Image in OpenCV Python | | OpenCV Tutorial in Hindi | Computer Vision: https://youtu.be/8gI3NSfCpKw
"""
import cv2
import numpy as np
import time
import random

Read and Show Image

img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\pexels-bess-hamiti-35188.jpg"

img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Add New single color pixel in a Image

img[0][0] ## Read pixel
# Add white pixels
img_copy = img.copy()
img_copy[0][0] = np.array([255,255,255])

cv2.imshow("Image", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Add white, black, Red, Green, Blue pixels
img_copy = img.copy()
img_copy[0][0] = np.array([255,255,255]) # White pixel
img_copy[0][1] = np.array([0,0,0]) #  Black pixel
img_copy[0][2] = np.array([0,0,255]) # Red pixel
img_copy[0][3] = np.array([0,255,0]) # Green Pixel
img_copy[0][4] = np.array([255,0,0]) # Blue pixel

cv2.imshow("Image", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()

Add New color pixel in a row of Image

img_copy = img.copy()

img_width= img_copy.shape[1]
img_height= img_copy.shape[0]

print("img_width: ", img_width)
print("img_height: ", img_height)

for cols_index in range(img_width):
    img_copy[0][cols_index] = np.array([255,255,255]) # White pixel
    img_copy[1][cols_index] = np.array([0,0,0]) #  Black pixel
    img_copy[2][cols_index] = np.array([0,0,255]) # Red pixel
    img_copy[3][cols_index] = np.array([0,255,0]) # Green Pixel
    img_copy[4][cols_index] = np.array([255,0,0]) # Blue pixel
    
cv2.imshow("Image", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()

Add New color pixel in a row of Image with logic/alternatively

img_copy = img.copy()

img_width= img_copy.shape[1]
img_height= img_copy.shape[0]

print("img_width: ", img_width)
print("img_height: ", img_height)

for cols_index in range(img_width):
    if cols_index%2 == 0:
        img_copy[0][cols_index] = np.array([255,255,255]) # White pixel
        img_copy[1][cols_index] = np.array([0,0,0]) #  Black pixel
        img_copy[2][cols_index] = np.array([0,0,255]) # Red pixel
        img_copy[3][cols_index] = np.array([0,255,0]) # Green Pixel
        img_copy[4][cols_index] = np.array([255,0,0]) # Blue pixel
    
cv2.imshow("Image", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()

Add New color pixel in a Image with logic/alternatively

img_copy = img.copy()

img_width= img_copy.shape[1]
img_height= img_copy.shape[0]

print("img_width: ", img_width)
print("img_height: ", img_height)

for rows_index in range(img_height):
    for cols_index in range(img_width):
        #if cols_index%2 == 0:
        if cols_index%50 == 0:
            #img_copy[rows_index][cols_index] = np.array([255,255,255]) # White pixel
            img_copy[rows_index][cols_index] = np.array([0,0,255]) # Red pixel

cv2.imshow("Image", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()
img_copy = img.copy()

img_width= img_copy.shape[1]
img_height= img_copy.shape[0]

print("img_width: ", img_width)
print("img_height: ", img_height)

for rows_index in range(img_height):
    if rows_index%2 == 0:
    #if rows_index%20 == 0:
        for cols_index in range(img_width):
            if cols_index%2 == 0:
            #if cols_index%50 == 0:
                img_copy[rows_index][cols_index] = np.array([255,255,255]) # White pixel
                #img_copy[rows_index][cols_index] = np.array([0,0,255]) # Red pixel

cv2.imshow("Image", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()

Add New random color pixel randomly in a Image

img_copy = img.copy()

img_width= img_copy.shape[1]
img_height= img_copy.shape[0]

print("img_width: ", img_width)
print("img_height: ", img_height)

for rows_index in range(img_height):
    if rows_index%2 == 0:
    #if rows_index%20 == 0:
        for cols_index in range(random.randint(0,1000)):
            #if cols_index%2 == 0:
            #if cols_index%50 == 0:
                b_pixel = random.randint(0,255)
                g_pixel = random.randint(0,255)
                r_pixel = random.randint(0,255)
                rand_cols_index = random.randint(0,img_width-1)
                img_copy[rows_index][rand_cols_index] = np.array([b_pixel,g_pixel,r_pixel]) # random pixel
                #img_copy[rows_index][cols_index] = np.array([255,255,255]) # white pixel

cv2.imshow("Image", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()
random.randint(0,255)

Add New color pixels in portion or any location of Image

img_copy = img.copy()

img_width= img_copy.shape[1]
img_height= img_copy.shape[0]

print("img_width: ", img_width)
print("img_height: ", img_height)

for rows_index in range(55, 700):
    if rows_index%2 == 0:
    #if rows_index%20 == 0:
        for cols_index in range(380, 950):
            if cols_index%2 == 0:
            #if cols_index%50 == 0:
                b_pixel = random.randint(0,255)
                g_pixel = random.randint(0,255)
                r_pixel = random.randint(0,255)
                #rand_cols_index = random.randint(0,img_width-1)
                #img_copy[rows_index][cols_index] = np.array([b_pixel,g_pixel,r_pixel]) # random pixel
                #img_copy[rows_index][cols_index] = np.array([255,255,255]) # white pixel
                img_copy[rows_index][cols_index] = np.array([255,0,0]) # blue pixel
cv2.imshow("Image", img_copy)
cv2.imwrite("effect_img.png", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()

The post Change the Pixel Value of an Image in OpenCV Python | OpenCV Tutorial appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/change-image-pixels-opencv-python/feed/ 0 1831
Get Image Pixels Using OpenCV Python | OpenCV Tutorial in Hindi https://indianaiproduction.com/get-image-opencv-python/ https://indianaiproduction.com/get-image-opencv-python/#respond Sun, 08 Aug 2021 08:16:09 +0000 https://indianaiproduction.com/?p=1818 In Python OpenCV Tutorial, Explained How to get image pixel using NumPy Slicing and indexing. How to access image pixels Below code executed on Jupyter Notebook Access Single Pixel of Image Show single pixel of Image as Image Access the row of Image Access the row of single & double channel Image Access the column of …

Get Image Pixels Using OpenCV Python | OpenCV Tutorial in Hindi Read More »

The post Get Image Pixels Using OpenCV Python | OpenCV Tutorial in Hindi appeared first on Indian AI Production.

]]>
In Python OpenCV Tutorial, Explained How to get image pixel using NumPy Slicing and indexing.

How to access image pixels

Below code executed on Jupyter Notebook

# Import Library

import cv2
import numpy as np

# Read & Show Image
img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\pexels-rafi-ahmed-haven-1253364.jpg"

img = cv2.imread(img_path)
img = cv2.resize(img,(int(img.shape[1]*0.5), int(img.shape[0]*0.5)))
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

img

img.shape

959*640

img.size

Access Single Pixel of Image

# # Access Single Pixel of Image

img[0,0]

img[0,0, 0]

img[0,0, 1]

img[0,0, 2]

img[0][0]

img[200,100]

img[900, 150]

img[180, 550]

Show single pixel of Image as Image

# # Show single pixel of Image as Image
cv2.imshow("image", img[900, 150])
cv2.waitKey(0)
cv2.destroyAllWindows()

Access the row of Image

img[0]

img[0].shape

img[0, :]

img[700, :]

Access the row of single & double channel Image

img[700, :, 2]

img[700, :, (0,1)]

img[700, :, (2,1,0)]

cv2.imshow("image", img[:, :, (1,0,2)])
cv2.waitKey(0)
cv2.destroyAllWindows()

Access the column of Image

img[:, 0]

img[:, 200]

Access the column of single & double channel Image

img[:, 200, 2]

img[:, 200, (1,2)]

img[:, 200, (1,2)].shape

Access the part of Image

img[80:400, 100:450] # [y1:y2, x1:x2] #, x1= 100, y1=80, x2= 450   y2= 400

cv2.imshow("image", img[80:400, 100:450])
cv2.waitKey(0)
cv2.destroyAllWindows()

x1 = 150, y1 = 850, x2 = 550, y2 = 900

cv2.imshow("image", img[750:900, 220:400])
cv2.imwrite("img_part.jpg", img[750:900, 220:400])
cv2.waitKey(0)
cv2.destroyAllWindows()

Show image Row wise

for i in range(img.shape[0]):
    cv2.imshow("image", img[0:i+1, :])

    if cv2.waitKey(27) & 0xff == ord('q'):
        break
cv2.destroyAllWindows()

Show image Column wise

for i in range(img.shape[1]):
    cv2.imshow("image", img[:, 0:i+1])

    if cv2.waitKey(27) & 0xff == ord('q'):
        break
cv2.destroyAllWindows()

The post Get Image Pixels Using OpenCV Python | OpenCV Tutorial in Hindi appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/get-image-opencv-python/feed/ 0 1818
How to Dilate(Dilation) Image using OpenCV Python https://indianaiproduction.com/image-dilation-opencv-python/ https://indianaiproduction.com/image-dilation-opencv-python/#respond Sun, 01 Aug 2021 06:23:56 +0000 https://indianaiproduction.com/?p=1812 Morphological Operations Morphological transformations are some simple operations based on the image shape. It is normally performed on binary images. It needs two inputs, one is our original image, the second one is called structuring element or kernel which decides the nature of the operation. Two basic morphological operators are Erosion and Dilation. Then its variant …

How to Dilate(Dilation) Image using OpenCV Python Read More »

The post How to Dilate(Dilation) Image using OpenCV Python appeared first on Indian AI Production.

]]>
Morphological Operations

Morphological transformations are some simple operations based on the image shape. It is normally performed on binary images.

It needs two inputs, one is our original image, the second one is called structuring element or kernel which decides the nature of the operation.

Two basic morphological operators are Erosion and Dilation. Then its variant forms like Opening, Closing, Gradient etc also comes into play. We will see them one-by-one with help of the following image:

Dilation

It is just the opposite of erosion. Here, a pixel element is ‘1’ if at least one pixel under the kernel is ‘1’. So it increases the white region in the image or the size of the foreground object increases. Normally, in cases like noise removal, erosion is followed by dilation. Because, erosion removes white noises, but it also shrinks our objects. So we dilate it. Since noise is gone, they won’t come back, but our object area increases. It is also useful in joining broken parts of an object.

Erosion Tutorial: https://indianaiproduction.com/image-erosion-opencv-python/

Syntax: cv2.dilate(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])
Return: Dilate Image

Parameters:

. @param src input image; the number of channels can be arbitrary, but the depth should be one of
. CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
. @param dst output image of the same size and type as src.
. @param kernel structuring element used for dilation; if elemenat=Mat(), a 3 x 3 rectangular
. structuring element is used. Kernel can be created using #getStructuringElement
. @param anchor position of the anchor within the element; default value (-1, -1) means that the
. anchor is at the element center.
. @param iterations number of times dilation is applied.
. @param borderType pixel extrapolation method, see #BorderTypes
. @param borderValue border value in case of a constant border
. @sa erode, morphologyEx, getStructuringElement

Dilation of Image Practical using OpenCV

# Import Libraries

import cv2
import numpy as np
# Read Image

img_path_road = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\road\road1.jpg"
img_path_girl_eye = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\girl-eye.jpg"
img_path_iaip = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\indian ai production name.png"

img_road = cv2.imread(img_path_road, 0)
img_iaip = cv2.imread(img_path_iaip, 0)
img_girl_eye = cv2.imread(img_path_girl_eye, 0)

img_road = cv2.resize(img_road, (600, 400))
img_girl_eye = cv2.resize(img_girl_eye, (600,400))

cv2.imshow("Image road", img_road)
cv2.imshow("Image iaip", img_iaip)
cv2.imshow("Image GIrl Eye", img_girl_eye)

cv2.waitKey(0)
cv2.destroyAllWindows()
# Create Kernel

kernel = np.ones((5,5), dtype = "uint8")
kernel
# Dilate girl Eye Image 

dilated_img = cv2.dilate(img_girl_eye, kernel, iterations=1)

img_girl_eye_dilated_img = np.hstack((img_girl_eye, dilated_img))

cv2.imshow("Image", img_girl_eye_dilated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Dilate Road Image

dilated_img = cv2.dilate(img_road, kernel, iterations=2)

img_girl_eye_dilated_img = np.hstack((img_road, dilated_img))

cv2.imshow("Image", img_girl_eye_dilated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Dilate text Image

dilated_img = cv2.dilate(img_iaip, kernel, iterations=1)

img_girl_eye_dilated_img = np.vstack((img_iaip, dilated_img))

cv2.imshow("Image", img_girl_eye_dilated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

REF: https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html

The post How to Dilate(Dilation) Image using OpenCV Python appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/image-dilation-opencv-python/feed/ 0 1812
How to Erode(Erosion) Image using OpenCV Python https://indianaiproduction.com/image-erosion-opencv-python/ https://indianaiproduction.com/image-erosion-opencv-python/#comments Sun, 01 Aug 2021 06:23:33 +0000 https://indianaiproduction.com/?p=1814 Morphological Operations Morphological transformations are some simple operations based on the image shape. It is normally performed on binary images. It needs two inputs, one is our original image, the second one is called structuring element or kernel which decides the nature of the operation. Two basic morphological operators are Erosion and Dilation. Then its variant …

How to Erode(Erosion) Image using OpenCV Python Read More »

The post How to Erode(Erosion) Image using OpenCV Python appeared first on Indian AI Production.

]]>
Morphological Operations

Morphological transformations are some simple operations based on the image shape. It is normally performed on binary images.

It needs two inputs, one is our original image, the second one is called structuring element or kernel which decides the nature of the operation.

Two basic morphological operators are Erosion and Dilation. Then its variant forms like Opening, Closing, Gradient etc also comes into play. We will see them one-by-one with help of the following image:

Erosion

The basic idea of erosion is just like soil erosion only, it erodes away the boundaries of the foreground object (Always try to keep foreground in white). So what it does? The kernel slides through the image (as in 2D convolution). A pixel in the original image (either 1 or 0) will be considered 1 only if all the pixels under the kernel are 1, otherwise, it is eroded (made to zero).

So what happens is that all the pixels near the boundary will be discarded depending upon the size of the kernel. So the thickness or size of the foreground object decreases or simply the white region decreases in the image. It is useful for removing small white noises (as we have seen in the colorspace chapter), detach two connected objects, etc.

Dilation Tutorial: https://indianaiproduction.com/image-dilation-opencv-python/

Syntax: cv2.erode(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])
Return: Erode Image

Parameters:

.   @param src input image; the number of channels can be arbitrary, but the depth should be one of
.   CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
.   @param dst output image of the same size and type as src.
.   @param kernel structuring element used for erosion; if `element=Mat()`, a `3 x 3` rectangular
.   structuring element is used. Kernel can be created using #getStructuringElement.
.   @param anchor position of the anchor within the element; default value (-1, -1) means that the
.   anchor is at the element center.
.   @param iterations number of times erosion is applied.
.   @param borderType pixel extrapolation method, see #BorderTypes
.   @param borderValue border value in case of a constant border
.   @sa  dilate, morphologyEx, getStructuringElement

Erosion of Image Practical using OpenCV

# Import Libraries

import cv2
import numpy as np
# Read Image

img_path_road = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\road\road1.jpg"
img_path_girl_eye = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\girl-eye.jpg"
img_path_iaip = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\indian ai production name.png"

img_road = cv2.imread(img_path_road, 0)
img_iaip = cv2.imread(img_path_iaip, 0)
img_girl_eye = cv2.imread(img_path_girl_eye, 0)

img_road = cv2.resize(img_road, (600, 400))
img_girl_eye = cv2.resize(img_girl_eye, (600,400))

cv2.imshow("Image road", img_road)
cv2.imshow("Image iaip", img_iaip)
cv2.imshow("Image GIrl Eye", img_girl_eye)

cv2.waitKey(0)
cv2.destroyAllWindows()
# Create Kernel

kernel = np.ones((5,5), dtype = "uint8")
kernel

# Erode girl eye image

erode_img = cv2.erode(img_girl_eye, kernel, iterations=1)

org_img_erode_img = np.hstack((img_girl_eye, erode_img))

cv2.imshow("Image", org_img_erode_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Erode road image

erode_img = cv2.erode(img_road, kernel, iterations=1)

org_img_erode_img = np.hstack((img_road, erode_img))

cv2.imshow("Image", org_img_erode_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Erode text image

erode_img = cv2.erode(img_iaip, kernel, iterations=2)

org_img_erode_img = np.vstack((img_iaip, erode_img))

cv2.imshow("Image", org_img_erode_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

REF: https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html

The post How to Erode(Erosion) Image using OpenCV Python appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/image-erosion-opencv-python/feed/ 1 1814
Blur Image using Gaussian Filter OpenCV Python | OpenCV Tutorial https://indianaiproduction.com/blur-image-using-gaussian-filter-opencv-python/ https://indianaiproduction.com/blur-image-using-gaussian-filter-opencv-python/#respond Wed, 12 May 2021 04:21:06 +0000 https://indianaiproduction.com/?p=1767 In Python OpenCV Tutorial, Explained How to Blur image using cv2.GaussianBlur() opencv function. Get the answers of below questions: How do I blur an image in OpenCV? How do you blur an image in Python? Why do we blur image? How do you blur part of a picture? Syntax: cv2.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType]]]) -> …

Blur Image using Gaussian Filter OpenCV Python | OpenCV Tutorial Read More »

The post Blur Image using Gaussian Filter OpenCV Python | OpenCV Tutorial appeared first on Indian AI Production.

]]>
In Python OpenCV Tutorial, Explained How to Blur image using cv2.GaussianBlur() opencv function.

Get the answers of below questions:

  1. How do I blur an image in OpenCV?
  2. How do you blur an image in Python?
  3. Why do we blur image?
  4. How do you blur part of a picture?

Syntax:

cv2.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType]]]) -> dst

.   @brief Blurs an image using a Gaussian filter.
 .   
 .   The function convolves the source image with the specified Gaussian kernel. In-place filtering is
 .   supported.

.   @param src input image; the image can have any number of channels, which are processed
.   independently, but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
.   @param dst output image of the same size and type as src.
.   @param ksize Gaussian kernel size. ksize.width and ksize.height can differ but they both must be
.   positive and odd. Or, they can be zero's and then they are computed from sigma.
.   @param sigmaX Gaussian kernel standard deviation in X direction.
.   @param sigmaY Gaussian kernel standard deviation in Y direction; if sigmaY is zero, it is set to be
.   equal to sigmaX, if both sigmas are zeros, they are computed from ksize.width and ksize.height,
.   respectively (see #getGaussianKernel for details); to fully control the result regardless of
.   possible future modifications of all this semantics, it is recommended to specify all of ksize,
.   sigmaX, and sigmaY.
.   @param borderType pixel extrapolation method, see #BorderTypes

Show Image

# Show Image
################## Video Tutorial: https://youtu.be/W05qMagCaR4
import cv2

img_path = r"D:\Indian AI Production\OpenCV\01 Opencv Tutorial\020 Blure Image\sandeep_maheshwari.jpg"

img = cv2.imread(img_path)

cv2.imshow("Model Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Blur Image using Gaussian Blur

# Normal Blur

blur_img = cv2.GaussianBlur(img, (3,3), sigmaX=34, sigmaY=36)

cv2.imshow("Blur Image", blur_img)
cv2.imshow("Model Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()
# Heavy Blur

blur_img = cv2.GaussianBlur(img, (101,101), sigmaX=40, sigmaY=30)

cv2.imshow("Blur Image", blur_img)
cv2.imshow("Model Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Stretch image Vertically

blur_img = cv2.GaussianBlur(img, (1,99), sigmaX=10, sigmaY=10)

cv2.imshow("Blur Image", blur_img)
cv2.imshow("Model Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Stretch image Horizontally

blur_img = cv2.GaussianBlur(img, (99,1), sigmaX=10, sigmaY=20)

cv2.imshow("Blur Image", blur_img)
cv2.imshow("Model Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Save Blur Image

cv2.imwrite("blur_img.jpg", blur_img)

The post Blur Image using Gaussian Filter OpenCV Python | OpenCV Tutorial appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/blur-image-using-gaussian-filter-opencv-python/feed/ 0 1767
Blur Image Using cv2.blur() & cv2.boxFilter()OpenCV Python | OpenCV Tutorial https://indianaiproduction.com/blur-image-using-cv2-blur-cv2-boxfilter-opencv-python/ https://indianaiproduction.com/blur-image-using-cv2-blur-cv2-boxfilter-opencv-python/#respond Fri, 07 May 2021 04:36:52 +0000 https://indianaiproduction.com/?p=1761 In Python OpenCV Tutorial, Explained How to Blur image using cv2.blur() and cv2.boxFilter() opencv function. Get the answers of below questions: How do I blur an image in OpenCV? How do you blur an image in Python? Why do we blur image? How do you blur part of a picture? Syntax: cv2.blur(src, ksize[, dst[, anchor[, borderType]]]) -> dst …

Blur Image Using cv2.blur() & cv2.boxFilter()OpenCV Python | OpenCV Tutorial Read More »

The post Blur Image Using cv2.blur() & cv2.boxFilter()OpenCV Python | OpenCV Tutorial appeared first on Indian AI Production.

]]>
In Python OpenCV Tutorial, Explained How to Blur image using cv2.blur() and cv2.boxFilter() opencv function.

Get the answers of below questions:

  1. How do I blur an image in OpenCV?
  2. How do you blur an image in Python?
  3. Why do we blur image?
  4. How do you blur part of a picture?

Syntax:

cv2.blur(src, ksize[, dst[, anchor[, borderType]]]) -> dst
@brief Blurs an image using the normalized box filter.

Parameters: 

@param src input image; it can have any number of channels, which are processed independently, but .   the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F. .   
@param dst output image of the same size and type as src. .   
@param ksize blurring kernel size. .   
@param anchor anchor point; default value Point(-1,-1) means that the anchor is at the kernel .   center. .   
@param borderType border mode used to extrapolate pixels outside of the image, see #BorderTypes @sa  boxFilter, bilateralFilter, GaussianBlur, medianBlur
cv2.boxFilter(src, ddepth, ksize[, dst[, anchor[, normalize[, borderType]]]]) -> dst
The function smooths an image using the kernel:

Parameters:

@param src input image. .   
@param dst output image of the same size and type as src. .   
@param ddepth the output image depth (-1 to use src.depth()). .   
@param ksize blurring kernel size. .   
@param anchor anchor point; default value Point(-1,-1) means that the anchor is at the kernel .   center. .   
@param normalize flag, specifying whether the kernel is normalized by its area or not. .   @param borderType border mode used to extrapolate pixels outside of the image, see #BorderTypes @sa  blur, bilateralFilter, GaussianBlur, medianBlur, integral

Read & Show Image

#### OpenCV Video Tutorial Playlist: https://www.youtube.com/watch?v=-rm0P7A4Jbc&list=PLfP3JxW-T70G5FB9vcmT6T3xnmvFvqV7w ####

## Show Image

import cv2

img_path = r"D:\Indian AI Production\OpenCV\01 Opencv Tutorial\020 Blure Image\vivek_bindra.jpg"
img = cv2.imread(img_path)

cv2.imshow("Model", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Blur Image using cv2.blur() with Kernel 3×3

img_blur_3 = cv2.blur(img, (3,3))

cv2.imshow("Model Blur", img_blur_3)
cv2.imshow("Model ", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Blur Image using cv2.blur() with Kernel 50×50

img_blur_50 = cv2.blur(img, (50,50))

cv2.imshow("Model Blur", img_blur_50)
cv2.imshow("Model ", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Blur Image using cv2.boxfilter() with Kernel 3×3

img_b_blur_3 = cv2.boxFilter(img, -1, (3,3))

cv2.imshow("Model Blur", img_b_blur_3)
cv2.imshow("Model ", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Blur Image using cv2.boxfilter() with Kernel 3×3 without Normalization

img_b_blur_3 = cv2.boxFilter(img, -1, (3,3), normalize=False)

cv2.imshow("Model Blur normalize", img_b_blur_3)
cv2.imshow("Model ", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Blur Image using cv2.boxfilter() with Kernel 50×50

img_b_blur_50 = cv2.boxFilter(img, -1, (50,50))

cv2.imshow("Model Blur", img_b_blur_50)
cv2.imshow("Model ", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Show All Blur Image

import numpy as np

img_blur_3 = cv2.resize(img_blur_3, (650, 400))
img_blur_50 = cv2.resize(img_blur_50, (650, 400))

img_b_blur_3 = cv2.resize(img_b_blur_3, (650, 400))
img_b_blur_50 = cv2.resize(img_b_blur_50, (650, 400))

#putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
cv2.putText(img_blur_3, "blur 3x3", (10,50), cv2.FONT_HERSHEY_COMPLEX, 2, (255,0,55), 3)
cv2.putText(img_blur_50, "blur 50x50", (10,50), cv2.FONT_HERSHEY_COMPLEX, 2, (255,0,55), 3)
cv2.putText(img_b_blur_3, "box blur 3x3", (10,50), cv2.FONT_HERSHEY_COMPLEX, 2, (255,255, 0), 3)
cv2.putText(img_b_blur_50, "box blur 50x50", (10,50), cv2.FONT_HERSHEY_COMPLEX, 2, (255,255, 0), 3)


blurr_2_img = np.hstack((img_blur_3, img_blur_50))
filter_blur_2_img = np.hstack((img_b_blur_3, img_b_blur_50))

img_4 = np.vstack((blurr_2_img, filter_blur_2_img))

cv2.imshow("Show 4 Blur Image", img_4)

cv2.waitKey(0)
cv2.destroyAllWindows()

Blur Image in 2 Line

import cv2

cv2.imshow("Blur Image", cv2.blur(cv2.imread(r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\girl-eye.jpg"), (50,50)))

cv2.waitKey(0)
cv2.destroyAllWindows()

The post Blur Image Using cv2.blur() & cv2.boxFilter()OpenCV Python | OpenCV Tutorial appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/blur-image-using-cv2-blur-cv2-boxfilter-opencv-python/feed/ 0 1761
Blur Image using filter2d OpenCV Python | OpenCV Tutorial https://indianaiproduction.com/blur-image-using-filter2d-opencv-python/ https://indianaiproduction.com/blur-image-using-filter2d-opencv-python/#respond Wed, 05 May 2021 02:46:28 +0000 https://indianaiproduction.com/?p=1758 In Python OpenCV Tutorial, Explained How to Blur image using cv2.filter2d() opencv function. Get the answers of below questions: How do I blur an image in OpenCV? How do you blur an image in Python? Why do we blur image? How do you blur part of a picture? Syntax: cv2.filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]]) …

Blur Image using filter2d OpenCV Python | OpenCV Tutorial Read More »

The post Blur Image using filter2d OpenCV Python | OpenCV Tutorial appeared first on Indian AI Production.

]]>
In Python OpenCV Tutorial, Explained How to Blur image using cv2.filter2d() opencv function.

Get the answers of below questions:

  1. How do I blur an image in OpenCV?
  2. How do you blur an image in Python?
  3. Why do we blur image?
  4. How do you blur part of a picture?

Syntax:

cv2.filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]]) -> dst

Parameters:
.   @param src input image.
.   @param dst output image of the same size and the same number of channels as src.
.   @param ddepth desired depth of the destination image, see @ref filter_depths "combinations"
.   @param kernel convolution kernel (or rather a correlation kernel), a single-channel floating point
.   matrix; if you want to apply different kernels to different channels, split the image into
.   separate color planes using split and process them individually.
.   @param anchor anchor of the kernel that indicates the relative position of a filtered point within
.   the kernel; the anchor should lie within the kernel; default value (-1,-1) means that the anchor
.   is at the kernel center.
.   @param delta optional value added to the filtered pixels before storing them in dst.
.   @param borderType pixel extrapolation method, see #BorderTypes
.   @sa  sepFilter2D, dft, matchTemplate

Blur Image using Filter2D with 3×3 kernel

#### OpenCV Video Tutorial Playlist: https://www.youtube.com/watch?v=-rm0P7A4Jbc&list=PLfP3JxW-T70G5FB9vcmT6T3xnmvFvqV7w ####

# Show Image
import cv2
import numpy as np

img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\adult-beauty-blond-301298.jpg"

img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))

cv2.imshow("Model Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()
one_mat_3_3 = np.ones((3,3), dtype=np.float32)/9
blur_img_3_3 = cv2.filter2D(img, -1, one_mat_3_3)


cv2.imshow("Blure image 3x3", blur_img_3_3)
cv2.imshow("Original Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Blur Image using Filter2D with 5×5 kernel

one_mat_5_5 = np.ones((5,5), dtype=np.float32)/25
blur_img_5_5 = cv2.filter2D(img, -1, one_mat_5_5)


cv2.imshow("Blure image 5x5", blur_img_5_5)
cv2.imshow("Original Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Blur Image using Filter2D with 10×10 kernel

# 10*10
one_mat_10_10 = np.ones((10,10), dtype=np.float32)/100

blur_img_10_10 = cv2.filter2D(img, -1, one_mat_10_10)

cv2.imshow("Original Image", img)
cv2.imshow("Blure Image using", blur_img_10_10)

cv2.waitKey(0)
cv2.destroyAllWindows()

Blur Image using Filter2D with 50×50 kernel

# 50*50
one_mat_50_50 = np.ones((50,50), dtype=np.float32)/2500

blur_img_50_50 = cv2.filter2D(img, -1, one_mat_50_50)

cv2.imshow("Original Image", img)
cv2.imshow("Blure Image using", blur_img_50_50)

cv2.waitKey(0)
cv2.destroyAllWindows()

Blur Image using Filter2D with 100×100 kernel

# 100*100
one_mat_100_100 = np.ones((100,100), dtype=np.float32)/10000

blur_img_100_100 = cv2.filter2D(img, -1, one_mat_100_100)

cv2.imshow("Original Image", img)
cv2.imshow("Blure Image using", blur_img_100_100)

cv2.waitKey(0)
cv2.destroyAllWindows()

Show All Result in Single Windows(Image)

## Show All image Result in Single Windows(Image)

img = cv2.resize(img, (400, 350))
blur_img_3_3 = cv2.resize(blur_img_3_3, (400, 350))
blur_img_5_5 = cv2.resize(blur_img_5_5, (400, 350))
blur_img_10_10 = cv2.resize(blur_img_10_10, (400, 350))
blur_img_50_50 = cv2.resize(blur_img_50_50, (400, 350))
blur_img_100_100 = cv2.resize(blur_img_100_100, (400, 350))

cv2.putText(img, "Original", org=(20,40), fontFace=cv2.FONT_HERSHEY_PLAIN, fontScale= 2 , color=(0,0,255))
cv2.putText(blur_img_3_3, "3x3 Filter", org=(20,40), fontFace=cv2.FONT_HERSHEY_PLAIN, fontScale= 2 , color=(255,0,255))
cv2.putText(blur_img_5_5, "5x5 Filter", org=(20,40), fontFace=cv2.FONT_HERSHEY_PLAIN, fontScale= 2 , color=(255,0,255))
cv2.putText(blur_img_10_10, "10x10 Filter", org=(20,40), fontFace=cv2.FONT_HERSHEY_PLAIN, fontScale= 2 , color=(255,0,255))
cv2.putText(blur_img_50_50, "50x50 Filter", org=(20,40), fontFace=cv2.FONT_HERSHEY_PLAIN, fontScale= 2 , color=(255,0,255))
cv2.putText(blur_img_100_100, "100x100 Filter", org=(20,40), fontFace=cv2.FONT_HERSHEY_PLAIN, fontScale= 2 , color=(255,0,255))

img1_3 = np.hstack((img, blur_img_3_3, blur_img_5_5))
img2_3 = np.hstack((blur_img_10_10, blur_img_50_50,blur_img_100_100))

img3_6 = np.vstack((img1_3,img2_3))

cv2.imshow("1 original and 5 Blur Image", img3_6)
cv2.waitKey(0)
cv2.destroyAllWindows()

The post Blur Image using filter2d OpenCV Python | OpenCV Tutorial appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/blur-image-using-filter2d-opencv-python/feed/ 0 1758
Color Pixels Extraction using OpenCV Python | OpenCV Tutorial https://indianaiproduction.com/pixels-color-extraction-using-opencv-python/ https://indianaiproduction.com/pixels-color-extraction-using-opencv-python/#respond Fri, 23 Apr 2021 13:37:57 +0000 https://indianaiproduction.com/?p=1753 In Python OpenCV Tutorial, Explained How to Extraction or Detect Pixels Color using OpenCV Python Get the answers of below questions: How do you find the color of the pixel of an image? How do you find the color of an image in Python? How do I extract color features from an image? How do we …

Color Pixels Extraction using OpenCV Python | OpenCV Tutorial Read More »

The post Color Pixels Extraction using OpenCV Python | OpenCV Tutorial appeared first on Indian AI Production.

]]>
In Python OpenCV Tutorial, Explained How to Extraction or Detect Pixels Color using OpenCV Python

Get the answers of below questions:

  1. How do you find the color of the pixel of an image?
  2. How do you find the color of an image in Python?
  3. How do I extract color features from an image?
  4. How do we find items of a specific color?
  5. How can I identify a color in an image?

Color Pixels Extraction

How to Detect Road Marking Using OpenCV

# Show an Image
import cv2
import numpy as np

img_path =r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\road\road1.jpg"

img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))

cv2.imshow("Road Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Conver image in gray scale

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow("Gray Image", gray_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Road Margin Detection using Gray Image

gray_img_copy = np.copy(gray_img)

gray_img_copy[gray_img_copy[:, :] < 200]=0

cv2.imshow("Gray Image", gray_img_copy)

cv2.waitKey(0)
cv2.destroyAllWindows()
gray_img ## 0 - 255 ## 0=Black, 255= White

gray_img_copy[:, :] < 200

gray_img_copy

gray_img_copy[:, 300]

Road Margin Detection using Color Image

img_copy = np.copy(img)

print("img_copy.shape: ", img_copy.shape)

img_copy[(img_copy[:, :, 0] < 200) | (img_copy[:, :, 1] < 200) | (img_copy[:, :, 2] < 200)] = 0

cv2.imshow("Color Image", img_copy)

cv2.waitKey(0)
cv2.destroyAllWindows()

Sign Board Detection

img_path =r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\road\road5.jpg"

img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))

img_copy = np.copy(img)

img_copy[(img_copy[:,:,0] > 50) | (img_copy[:,:,1] < 150) | (img_copy[:, :, 2] < 150) ]=0

img_2 = np.hstack((cv2.resize(img, (650, 500)), cv2.resize(img_copy, (650, 500))))
cv2.imshow("Yellow Road Image", img_2)

cv2.waitKey(0)
cv2.destroyAllWindows()

Red Color Pixels Extraction

img_path =r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\road\road6.jpg"

img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))

img_copy = np.copy(img)

img_copy[(img_copy[:,:,0] > 50) | (img_copy[:,:,1] > 50) | (img_copy[:, :, 2] < 90) ]=0

img_2 = np.hstack(( cv2.resize(img, (650, 500)), cv2.resize(img_copy, (650, 500)) ))
cv2.imshow("Color Image VS Color Extracted Image", img_2)

cv2.waitKey(0)
cv2.destroyAllWindows()

Yellow Color Pixels Extraction

img_path =r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\road\road4.jpg"

img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))

img_copy = np.copy(img)

img_copy[(img_copy[:,:,0] > 50) | (img_copy[:,:,1] < 150) | (img_copy[:, :, 2] < 150) ]=0
cv2.imshow("Yellow Road Image", img_copy)

cv2.waitKey(0)
cv2.destroyAllWindows()

Human Detection

img_path =r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\road\sunset.jpg"

img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))

img_copy = np.copy(img)

img_copy[(img_copy[:,:,0] > 90) | (img_copy[:,:,1] > 90) | (img_copy[:, :, 2] > 90) ]=255

img_2 = np.hstack((cv2.resize(img, (650, 500)), cv2.resize(img_copy, (650, 500))))
cv2.imshow("Yellow Road Image", img_2)

cv2.waitKey(0)
cv2.destroyAllWindows()

The post Color Pixels Extraction using OpenCV Python | OpenCV Tutorial appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/pixels-color-extraction-using-opencv-python/feed/ 0 1753
How to Show Histogram of Image using OpenCV Python | OpenCV Tutorial https://indianaiproduction.com/show-histogram-of-image-using-opencv-python/ https://indianaiproduction.com/show-histogram-of-image-using-opencv-python/#respond Sun, 18 Apr 2021 09:35:58 +0000 https://indianaiproduction.com/?p=1748 In Python OpenCV Tutorial, Explained How to show the Histogram of Image to analyse the image color format using OpenCV Python Get the answers of below questions: How do you find the histogram of an image? How do you find the histogram of an image in Python? Green channel extraction in image processing python What is …

How to Show Histogram of Image using OpenCV Python | OpenCV Tutorial Read More »

The post How to Show Histogram of Image using OpenCV Python | OpenCV Tutorial appeared first on Indian AI Production.

]]>
In Python OpenCV Tutorial, Explained How to show the Histogram of Image to analyse the image color format using OpenCV Python

Get the answers of below questions:

  1. How do you find the histogram of an image?
  2. How do you find the histogram of an image in Python?
  3. Green channel extraction in image processing python
  4. What is histogram in OpenCV?
  5. How do you plot a histogram of a picture?
  6. How do you calculate a histogram?
  7. What are the properties of histogram?

What is histogram ?

You can consider histogram as a graph or plot, which gives you an overall idea about the intensity distribution of an image. It is a plot with pixel values (ranging from 0 to 255, not always) in X-axis and corresponding number of pixels in the image on Y-axis.

Syntax: cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) -> hist

1. Histogram Calculation in OpenCV
So now we use cv.calcHist() function to find the histogram. Let's familiarize with the function and its parameters :

cv.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])
images : it is the source image of type uint8 or float32. it should be given in square brackets, 
    ie, "[img]".
channels : it is also given in square brackets. It is the index of channel for which we calculate 
    histogram. 
    For example, if input is grayscale image, its value is [0]. For color image, you can pass [0],
    [1] or [2] to calculate histogram of blue, green or red channel respectively.
mask : mask image. To find histogram of full image, it is given as "None". But if you want to find 
    histogram of particular region of image, you have to create a mask image for that and give it 
    as mask. (I will show an example later.)
histSize : this represents our BIN count. Need to be given in square brackets. For full scale,
    we pass [256].
ranges : this is our RANGE. Normally, it is [0,256].
So let's start with a sample image. Simply load an image in grayscale mode and find its full histogram.

REF: https://docs.opencv.org/master/d1/db7/tutorial_py_histogram_begins.html

Code(Jupyter Notebook):

## Get Prime Video free at
## OpenCV Tutorial Playlist: https://www.youtube.com/watch?v=-rm0P7A4Jbc&list=PLfP3JxW-T70G5FB9vcmT6T3xnmvFvqV7w

# # How to Show Histogram of Image

# In[1]:
# Show Image
import cv2
import matplotlib.pyplot as plt

img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\banner-1235604.jpg"
#img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\green.jpg"
img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))

cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

# In[2]:
img

# In[3]:
img.ravel()

# In[4]:
img.shape

# In[5]:
img.ravel().shape

# In[6]:
img.ravel().size

# ## Histogram of Image 
# In[7]:
plt.hist(img.ravel(), bins=256, range = [0,255])
plt.show()

# ## Histogram of All channels
# In[8]:
colors = ('b', 'g', 'r')

img_ravel = [img[:, :, 0].ravel(), img[:, :, 1].ravel(), img[:,:, 2].ravel()] 
plt.hist(img_ravel, color=colors, label=colors)
plt.legend()
plt.show()

# In[9]:
colors = ('b', 'g', 'r')
img_ravel = [img[:, :, 0].ravel(), img[:, :, 1].ravel(), img[:,:, 2].ravel()] 
plt.hist(img_ravel, color=colors, label=colors, bins=256, range=[0,256])
plt.legend()
plt.show()

# ## Plot of all Channel using cv2.calcHist() Function
# In[10]:
colors = ('b', 'g', 'r')      
plt.figure(figsize=(16, 9))
for i, color in enumerate(colors):
    histogram = cv2.calcHist([img], [i], None, [256], [0, 256])
    plt.plot(histogram, color=color)
plt.show()

# In[11]:
histogram

The post How to Show Histogram of Image using OpenCV Python | OpenCV Tutorial appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/show-histogram-of-image-using-opencv-python/feed/ 0 1748