[OpenCV] expands the image boundary and opencv expands the image boundary.

Source: Internet
Author: User

[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.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.