[OpenCV] expands the image boundary and opencv expands the image boundary.
In image processing, some attribute values at the current pixel position are often calculated using the adjacent pixels of the current pixel. This will lead to cross-border access to the border pixel. Generally, there are two ways to solve this problem: this article mainly introduces how to use OpenCV to expand the border.
Boundary Expansion Method
OpenCV provides several different boundary expansion strategies:
* BORDER_REPLICATE: aaaaaa|abcdefgh|hhhhhhh* BORDER_REFLECT: fedcba|abcdefgh|hgfedcb* BORDER_REFLECT_101: gfedcb|abcdefgh|gfedcba* BORDER_WRAP: cdefgh|abcdefgh|abcdefg* BORDER_CONSTANT: iiiiii|abcdefgh|iiiiiii with some specified ’i’
The above content is taken from the help document of OpenCV. "|" Indicates the boundary of the image, and the link "|" indicates the content of the image. The last boundary expansion policy also needs to specify an I value, it is used to assign values to additional boundaries.
Boundary Expansion
Use the copyMakeBorder () function provided by OpenCV to expand the boundary. Its prototype is as follows:
void copyMakeBorder( InputArray src, OutputArray dst, int top, int bottom, int left, int right, int borderType, const Scalar& value=Scalar() )
Src: Input array.
Dst: the array after the expanded boundary of the output.
Top: number of rows that are expanded up on the src upper boundary.
Bottom: number of rows that are extended downward under the src boundary.
Left: Number of columns expanded to the left at the left border of src.
Right: Number of columns expanded to the right of src.
BorderType: One of the boundary expansion strategies in the previous section.
Value: when your border policy uses BORDER_CONSTANT, this refers to the constant value entered at the border.
Instance
Use a simple example to explain how to expand the border.
Mat extendedIm;copyMakeBorder( orgIm, extendedIm, extRows, extRows, extCols, extCols, BORDER_REFLECT_101, Scalar::all(0) );
In the instance, the upper and lower boundary expand extRows rows, and the left and right expand extCols columns respectively. BORDER_REFLECT_101 is used. There is one parameter at most, which can be left unspecified, I wrote it here to tell you that the function can fill in these parameters.
The following is the experiment code and result:
#include <iostream>#include <opencv2/opencv.hpp>using namespace std;using namespace cv;int main(int argc, char**argv){Mat orgIm = imread("theImage.png");int extRows = 19;int extCols = 15;Mat extendedIm;copyMakeBorder( orgIm, extendedIm, extRows, extRows, extCols, extCols, BORDER_REFLECT_101);imshow("original image", orgIm);imshow("extended image", extendedIm);waitKey();return 0;}
Is the original image.
Is the expanded image.