SoFunction
Updated on 2024-11-15

opencv3/C++ image pixel manipulation in detail

RGB image to grayscale

Commonly used when converting RGB images to grayscale:

To perform the conversion, the following attempts are made to convert an RGB image to a grayscale image by other means of manipulation of the image pixels.

#include<opencv2/>
#include<>
using namespace cv;

int main()
{
 //Pixel manipulation
 Mat src,dst;
 src = imread("E:/image/image/");
 if(())
 {
  printf("can not load image \n");
  return -1;
 }

 namedWindow("input");
 imshow("input",src);

 ((), ());

 for(int row = 0; row < ; row++)
 {
  for(int col = 0; col < ; col++)
  {
   int b = <Vec3b>(row, col)[0];
   int g = <Vec3b>(row, col)[1];
   int r = <Vec3b>(row, col)[2];
   <Vec3b>(row, col)[0] = max(r,max(g,b));
   <Vec3b>(row, col)[1] = max(r,max(g,b));
   <Vec3b>(row, col)[2] = max(r,max(g,b));

  }
 }

 namedWindow("output");
 imshow("output",dst);
 waitKey();

}

Similarly using min(r,min(g,b)) one can see that the image is significantly darker due to the choice of smaller gray values:

Image Linear Enhancement

Linear enhancement of an image is achieved by manipulating (linearly transforming) the image pixels.

#include<opencv2/>
#include<>
using namespace cv;

int main()
{
 Mat src1, dst;
 src1 = imread("E:/image/image/");
 if(())
 {
  printf("can not load im1 \n");
  return -1;
 }
 double alpha = 1.2, beta = 50;
 dst = Mat::zeros((), ());
 for(int row = 0; row < ; row++)
 {
  for(int col = 0; col < ; col++)
  {
   if(() == 3)
   {
    int b = <Vec3b>(row, col)[0]; 
    int g = <Vec3b>(row, col)[1]; 
    int r = <Vec3b>(row, col)[2]; 

    <Vec3b>(row, col)[0] = saturate_cast<uchar>(b*alpha + beta); 
    <Vec3b>(row, col)[1] = saturate_cast<uchar>(g*alpha + beta); 
    <Vec3b>(row, col)[2] = saturate_cast<uchar>(r*alpha + beta); 
   }
   else if (() == 1)
   {
    float v = <uchar>(row, col); 
    <uchar>(row, col) = saturate_cast<uchar>(v*alpha + beta);
   }
  }
 }

 namedWindow("output",CV_WINDOW_AUTOSIZE);
 imshow("output", dst);
 waitKey();
 return 0;
}

Mask operation to adjust image contrast

Image contrast is enhanced using a 3×3 mask:

#include<opencv2/>
#include<>
using namespace cv;

int main()
{
 Mat src, dst;
 src = imread("E:/image/image/");
 CV_Assert(() == CV_8U);
 if(!)
 {
  printf("can not load image \n");
  return -1;
 }

 (dst);
 for(int row = 1; row<( - 1); row++)
 {
  const uchar* previous = <uchar>(row - 1);
  const uchar* current = <uchar>(row);
  const uchar* next = <uchar>(row + 1);
  uchar* output = <uchar>(row);
  for(int col = (); col < ( - 1)*(); col++)
  {
   *output = saturate_cast<uchar>(9 * current[col] - 2*previous[col] - 2*next[col] - 2*current[col - ()] - 2*current[col + ()]);
   output++;
  }
 }

 namedWindow("image", CV_WINDOW_AUTOSIZE);
 imshow("image",dst);
 waitKey();
 return 0;
}

pixel remapping

Implement pixel remapping with cv::remap;

Description of the cv::remap parameter:

Remap(
InputArray src,// Input image
OutputArray dst,// Output image
InputArray map1,// Mapping table 1 (CV_32FC1/CV_32FC2)
InputArray map2,// Mapping Table 2 (CV_32FC1/CV_32FC2)
int interpolation,// Selected interpolation
int borderMode,// Boundary type (BORDER_CONSTANT)
const Scalar borderValue// Color
)

Interpolation Methods:

CV_INTER_NN =0, 
CV_INTER_LINEAR =1, 
CV_INTER_CUBIC =2, 
CV_INTER_AREA =3, 
CV_INTER_LANCZOS4 =4

Vertical flip of the image by pixel remapping:

#include<opencv2/>
using namespace cv;

int main()
{
 Mat src,dst;
 src = imread("E:/image/image/");
 if(())
 {
  printf("can not load image \n");
  return -1;
 }
 namedWindow("input", CV_WINDOW_AUTOSIZE);
 imshow("input", src);
 Mat mapx,mapy;
 ((), CV_32FC1);
 ((), CV_32FC1);
 for(int row = 0; row < ; row++)
 {
  for(int col = 0; col < ; col++)
  {
   <float>(row, col) = col;
   <float>(row, col) =  - row - 1;
  }
 }
 remap(src, dst, mapx, mapy, CV_INTER_NN, BORDER_CONSTANT, Scalar(0,255,255));

 namedWindow("output", CV_WINDOW_AUTOSIZE);
 imshow("output",dst);
 waitKey();
 return 0;
}

Above this opencv3/C++ image pixel operations in detail is all that I have shared with you, I hope to give you a reference, and I hope you will support me more.