इस tutorial में हम सीखेंगे कि opencv4nodejs पैकेज का उपयोग करके किसी image को grey scale में कैसे convert किया जाए।


Image को grey scale में कैसे convert करें

OpenCV के किसी भी function का उपयोग करने के लिए सबसे पहले हमें  opencv4nodejs module को load करना पड़ेगा। निचे दी हुई लाइन OpenCV को load करेगी।

 
const cv = require('opencv4nodejs');


एक बार OpenCV लोड करने के बाद हम उस इमेज को read करेंगे जिसे हम grey scale में convert करना चाहते है। OpenCV इस्तेमाल करने के लिए आपको cv इस keyword को इस्तेमाल करना पड़ेगा, जो original photo को read करने के लिए और इसे greyscale में परिवर्तित करने के लिए आवश्यक चीजे उपलब्ध कराएगा।


original photo को read करने के लिए, बस cv मॉड्यूल के imread() function को कॉल करें, image path को string के रूप में इस्तेमाल करें।

var image = cv.imread('C:/Users/Admin/Desktop/photo.jpg')

अब अगला स्टेप है, इस image को greyscale mode में convert करना। इसके लिए हमें OpenCv का bgrToGray function इस्तेमाल करना पड़ेगा, जो हमें साधारण रंगीन image से greyscale इमेज में convert करने में मदद करेगा।

यह function कोई argument नहीं लेती है और यह grey scale image  के साथ Mat Class के किसी अन्य object के output के रूप में वापस आती है।

const greyImg  = originalImage.bgrToGray();

अब हम इस grey scale image को अपने window में दिखाएंगे। इसके लिए हम OpenCV के imshow() function का इस्तेमाल करेंगे।

cv.imshow('Grey Image', greyImg);  
  

साथ में हम original image भी दिखाएंगे जिससे हमें comparison करने में आसानी हो। 

cv.imshow('Original Image', image);
    

आखिर में हम waitKey function  इस्तेमाल करेंगे, जो execution को तब तक रोकेगा जब तक हम किसी key पर क्लिक नहीं करते।

cv.waitKey();
    

Final Code

अंत में code कुछ ऐसा होगा। 

/* step I - loading OpenCV module */
const cv = require('opencv4nodejs');
 
/* Step II - Loading of Image */
const image = cv.imread('C:/Users/Admin/Desktop/photo.jpg');
 
/* Conversion - RGB to Grey */
const greyImg = image.bgrToGray();
 
/* showing of images RGB and GREY*/
cv.imshow('Original Image', image);
cv.imshow('Grey Image', greyImg);
 
/* calling of waitKey function */
cv.waitKey();
   

Output

Original Image

OpenCV RGB to GreyScale - Original Image

Grey Image

OpenCV RGB to GreyScale - Grey Image