Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> C++

C++ का उपयोग करके OpenCV में मल्टीचैनल छवि से पिक्सेल मान को कैसे पढ़ा जाए?

हमने 'ब्लू_चैनल', 'ग्रीन_चैनल' और 'रेड_चैनल' नामक तीन चर घोषित किए हैं। इन चरों का लक्ष्य पिक्सेल मानों को सहेजना है। हमने इन वेरिएबल्स को 'फॉर लूप्स' के अंदर इस्तेमाल किया है। फिर हमने 'color_Image_Matrix' नाम का एक मैट्रिक्स घोषित किया।

इस विधि का सिंटैक्स है:

blue_Channel = color_image_Matrix.at<Vec3b>(i, j)[0];

हमने एक बीजीआर छवि का इस्तेमाल किया। इसके तीन चैनल हैं। ये चैनल विशिष्ट अनुक्रम बनाए रखते हैं, color_image_Matrix.at (i, j) (i,i) पर स्थित पिक्सेल मानों का प्रतिनिधित्व करता है और [0] पहले चैनल का प्रतिनिधित्व करता है। उदाहरण के लिए, यदि हम पंक्ति को इस प्रकार लिखते हैं:

blue_Channel=color_image_Matrix.at<Vec3b> (30, 35) [0];

इसका मतलब है कि चर 'blue_Channel' में पहले चैनल का पिक्सेल मान (30, 35) पर स्थित होगा। इस प्रकार हम OpenCV का उपयोग करके पिक्सेल मानों तक पहुँच प्राप्त कर सकते हैं।

निम्न प्रोग्राम विभिन्न RGB छवियों के पिक्सेल मानों को पढ़ता है और कंसोल विंडो में विभिन्न चैनल पिक्सेल के मान को प्रदर्शित करता है।

उदाहरण

#include<iostream>
#include<opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main() {
   int blue_Channel;
   int green_Channel;
   int red_Channel;
   Mat color_image_Matrix; //Declaring a matrix to load the image//
   color_image_Matrix = imread("colors.jpg"); //loading image in the matrix//
   //Beginning of for loop to read pixel values of blue channel//
   for (int i = 0; i < color_image_Matrix.rows; i++)//loop for rows// {
      for (int j = 0; j < color_image_Matrix.cols; j++) {
         //loop for columns//
         blue_Channel = color_image_Matrix.at<Vec3b>(i, j)[0];
         //To read the value of first channel.Here the blue channel is first channel//
         cout << "Value of pixel of blue channel" << "(" << i << "," << j << ")" << "="
            << blue_Channel << endl; //showing the values in console window//
      }
   }
   //End of for loop to read pixel values of blue channel//
   //Beginning of for loop to read pixel values of green channel//
   for (int i = 0; i < color_image_Matrix.rows; i++)//loop for rows// {
      for (int j = 0; j < color_image_Matrix.cols; j++)//loop for columns// {
         green_Channel = color_image_Matrix.at<Vec3b>(i, j)[1];
         //To read the value of first channel.Here the green channel is first channel//
         cout << "Value of pixel of green channel" << "(" << i << ","
            << j << ")" << "=" << blue_Channel << endl;//showing the values in console window//
      }
   }
   //End of for loop to read pixel values of green channel//
   //Beginning of for loop to read pixel values of red channel//
   for (int i = 0; i < color_image_Matrix.rows; i++)//loop for rows// {
      for (int j = 0; j < color_image_Matrix.cols; j++)//loop for columns// {
         red_Channel = color_image_Matrix.at<Vec3b>(i, j)[2];
         //To read the value of first channel.Here the red channel is first channel//
         cout << "Value of pixel of red channel" << "(" << i << "," <<
            j << ")" << "=" << blue_Channel << endl; //showing the values in console window//
      }
   }
   //End of for loop to read pixel values of red channel//
   if (waitKey(0)==27);
      cout << "Image read successfully…!";
      return 0;
}

आउटपुट

Image read successfully...

इस प्रोग्राम को चलने में कई मिनट लगते हैं। यह विभिन्न चैनलों के प्रत्येक पिक्सेल मान को पढ़ता है। इसलिए पूरा परिणाम दिखाने में कुछ मिनट लगते हैं।


  1. सी ++ का उपयोग कर ओपनसीवी में बाइनरी छवि कैसे बनाएं?

    एक द्विआधारी छवि सिर्फ एक डिजिटल छवि है जो दो रंगों, काले और सफेद का प्रतिनिधित्व करती है। इमेज प्रोसेसिंग के नजरिए से, बाइनरी इमेज में दो संभावित मानों वाले पिक्सल होते हैं- शून्य और एक। जब पिक्सेल का मान 0 होता है, तो यह एक शुद्ध काले रंग का प्रतिनिधित्व करता है। जब पिक्सेल का मान 1 होता है, तो इस

  1. सी ++ का उपयोग कर ओपनसीवी में किसी छवि के चैनलों की संख्या की गणना कैसे करें?

    इस विषय में, हम समझेंगे कि किसी छवि के चैनलों की संख्या का पता कैसे लगाया जाए। प्रोग्राम चलाने के बाद, कंसोल विंडो में चैनल का नंबर दिखाया जाएगा। चैनल की संख्या प्राप्त करने के लिए, हमने ओपनसीवी के एक वर्ग का उपयोग किया है जिसका नाम चैनल () है। जब हम इमेज मैट्रिक्स को चैनल () वर्ग के ऑब्जेक्ट के रू

  1. Java OpenCV लाइब्रेरी का उपयोग करके किसी छवि की चमक को कैसे बदलें?

    कन्वर्ट टू () org.opencv.core.Mat . की विधि क्लास किसी इमेज के कंट्रास्ट और ब्राइटनेस को बदलने के लिए दिए गए मैट्रिक्स पर जरूरी कैलकुलेशन करता है। यह विधि 4 पैरामीटर स्वीकार करती है - चटाई -रिक्त मैट्रिक्स परिणाम को समान आकार और स्रोत मैट्रिक्स के प्रकार के साथ रखने के लिए। rtype - आउटपुट मैट्