Image processing often encountered using the current pixel neighbors to calculate the current pixel location of some property values, which will result in boundary pixels at the cross-border access, there are generally two ways to solve this problem: only the non-crossed pixels are processed, the image boundaries are expanded, This article mainly introduces how to use OpenCV to expand the boundary easily.
Ways to expand boundaries
OPENCV provides several different boundary extension 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 OpenCV's help documentation. where "|" represents the boundary of the image, even a "|" The middle is the content of the image, and the last boundary extension strategy is given an additional I value for assigning additional boundaries.
Boundary expansion
Use the function Copymakeborder () provided by OPENCV to extend the boundary, which is prototyped as follows
void Copymakeborder (Inputarray src, outputarray dst, int top, int bottom, int left, int. right, int bordertype, const Scal ar& value=scalar ())
SRC: the input array.
DST: An array of extended boundaries after the output.
Top: The number of rows that have been expanded upward on the SRC boundary.
Bottom: The number of rows that are expanded downward at the SRC lower boundary.
Left: The number of columns that extend left in Src.
Right: The number of columns extending right to the left of SRC.
Bordertype: One of the boundary extension strategies in the previous section.
Value: When your boundary policy is using border_constant, here is the constant value that is filled in at the boundary.
Instance
Use a simple example to explain how to extend the boundary.
Mat Extendedim;copymakeborder (Orgim, Extendedim, Extrows, Extrows, Extcols, Extcols, border_reflect_101, Scalar::all (0 ) );
In the instance, the upper and lower boundary expands extrows row respectively, the left and right respectively expands the Extcols column, uses is BORDER_REFLECT_101, the most has one parameter, may not write, I write here is to tell everybody function can fill these parameters.
Here are the experiment codes and results:
#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.
[OpenCV] Expanding image boundaries