GithubHelp home page GithubHelp logo

bharadwaj2004 / color_conversions_of-image Goto Github PK

View Code? Open in Web Editor NEW

This project forked from swedha333/color_conversions_of-image

0.0 0.0 0.0 19 KB

License: GNU General Public License v3.0

Python 100.00%

color_conversions_of-image's Introduction

COLOR_CONVERSIONS_OF-IMAGE

AIM

To write a python program using OpenCV to do the following image manipulations.

i) Read, display, and write an image.

ii) Access the rows and columns in an image.

iii) Cut and paste a small portion of the image.

iv)To perform the color conversion between RGB, BGR, HSV, and YCbCr color models.

Software Required:

Anaconda - Python 3.7

Algorithm:

Step1:

Choose an image and save it as a filename.jpg ,

Step2:

Use imread(filename, flags) to read the file.

Step3:

Use imshow(window_name, image) to display the image.

Step4:

Use imwrite(filename, image) to write the image.

Step5:

End the program and close the output image windows.

Step6:

Convert BGR and RGB to HSV and GRAY

Step7:

Convert HSV to RGB and BGR

Step8:

Convert RGB and BGR to YCrCb

Step9:

Split and Merge RGB Image

Step10:

Split and merge HSV Image

Program:

Developed By: B.Venkata bharadwaj

Register Number: 212222240020

Output:

i) Read and display the image

    import cv2
    image=cv2.imread('virat.jpg',1)
    image=cv2.resize(image,(400,300))
    cv2.imshow('virat',image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Output:

Screenshot 2024-02-16 094827

ii)Write the image

    import cv2
    image=cv2.imread('virat.jpg',0)
    cv2.imwrite('king kholi.jpg',image)

Output:

Screenshot 2024-02-16 094839

iii)Shape of the Image

    import cv2
    image=cv2.imread('virat.jpg',1)
    print(image.shape)

Output:

Screenshot 2024-02-16 094854

iv)Access rows and columns

    import random
    import cv2
    image=cv2.imread('virat.jpg',1)
    image=cv2.resize(image,(400,400))
    for i in range (150,200):
      for j in range(image.shape[1]):
          image[i][j]=[random.randint(0,255),
                       random.randint(0,255),
                       random.randint(0,255)] 
    cv2.imshow('king kholi',image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Output:

Screenshot 2024-02-16 102322

v)Cut and paste portion of image

   import cv2
   image=cv2.imread('virat.jpg',1)
   image=cv2.resize(image,(400,400))
   tag =image[130:200,110:190]
   image[110:180,120:200] = tag
   cv2.imshow('kingkholi1',image)
   cv2.waitKey(0)
   cv2.destroyAllWindows()

Output:

Screenshot 2024-02-16 102418

vi) BGR and RGB to HSV and GRAY

import cv2
img = cv2.imread('virat.jpg',1)
img = cv2.resize(img,(300,200))
cv2.imshow('Original Image',img)
hsv1 = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow('BGR2HSV',hsv1)
hsv2 = cv2.cvtColor(img,cv2.COLOR_RGB2HSV)
cv2.imshow('RGB2HSV',hsv2)
gray1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('BGR2GRAY',gray1)
gray2 = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
cv2.imshow('RGB2GRAY',gray2)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Screenshot 2024-02-16 102627 Screenshot 2024-02-16 102655 Screenshot 2024-02-16 102701 Screenshot 2024-02-16 102706 Screenshot 2024-02-16 102711

vii) HSV to RGB and BGR

import cv2
img = cv2.imread('virat.jpg')
img = cv2.resize(img,(300,200))
img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow('Original HSV Image',img)
RGB = cv2.cvtColor(img,cv2.COLOR_HSV2RGB)
cv2.imshow('2HSV2BGR',RGB)
BGR = cv2.cvtColor(img,cv2.COLOR_HSV2BGR)
cv2.imshow('HSV2RGB',BGR)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Screenshot 2024-02-16 102830 Screenshot 2024-02-16 102836 Screenshot 2024-02-16 102841

viii) RGB and BGR to YCrCb

import cv2
img = cv2.imread('virat.jpg')
img = cv2.resize(img,(300,200))
cv2.imshow('Original RGB Image',img)
YCrCb1 = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
cv2.imshow('RGB-2-YCrCb',YCrCb1)
YCrCb2 = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
cv2.imshow('BGR-2-YCrCb',YCrCb2)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Screenshot 2024-02-16 102941 Screenshot 2024-02-16 102945 Screenshot 2024-02-16 102952

ix) Split and merge RGB Image

import cv2
img = cv2.imread('virat.jpg',1)
img = cv2.resize(img,(300,200))
R = img[:,:,2]
G = img[:,:,1]
B = img[:,:,0]
cv2.imshow('R-Channel',R)
cv2.imshow('G-Channel',G)
cv2.imshow('B-Channel',B)
merged = cv2.merge((B,G,R))
cv2.imshow('Merged RGB image',merged)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Screenshot 2024-02-16 103123 Screenshot 2024-02-16 103127 Screenshot 2024-02-16 103133 Screenshot 2024-02-16 103137

x) Split and merge HSV Image

import cv2
img = cv2.imread("virat.jpg",1)
img = cv2.resize(img,(300,200))
img=cv2.cvtColor(img,cv2.COLOR_RGB2HSV)
H,S,V=cv2.split(img)
cv2.imshow('Hue',H)
cv2.imshow('Saturation',S)
cv2.imshow('Value',V)
merged = cv2.merge((H,S,V))
cv2.imshow('Merged',merged)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Screenshot 2024-02-16 103249 Screenshot 2024-02-16 103256 Screenshot 2024-02-16 103301 Screenshot 2024-02-16 103306

Result:

Thus the images are read, displayed, and written ,and color conversion was performed between RGB, HSV and YCbCr color models successfully using the python program.

color_conversions_of-image's People

Contributors

swedha333 avatar bharadwaj2004 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.