Skip to content

Commit 0cab203

Browse files
committed
Merge pull request opencv#3 from VladVin/fix/vignette
Fixed the vignette filter
2 parents 8fc45c5 + 3cfe60a commit 0cab203

File tree

4 files changed

+27
-35
lines changed

4 files changed

+27
-35
lines changed

modules/photoeffects/doc/vignette.rst

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,25 @@ Vignette
44

55
Makes the edges of the photo less bright, creating an oval frame around its central part.
66

7-
.. cpp:function:: int vignette(cv::InputArray src, cv::OutputArray dst, cv::Size rect)
7+
.. ocv:function:: void vignette(cv::InputArray src, cv::OutputArray dst, cv::Size rect)
88
9-
:param src: Source 8-bit three-channel image.
9+
:param src: Source 3-channel image.
1010
:param dst: Destination image of the same size and the same type as **src**.
11-
:param rect: Size of rectangle describes an ellipse, whose center is at the center image.
12-
:return: Error code.
11+
:param rect: Size of rectangle describes an ellipse, whose center is at the center of an image.
1312

1413
The algorithm.
1514

16-
#. Create a new 3-channel image, which is interpreted as BGR image.
15+
For every pixel of the **src** image calculate :math:`dist` and :math:`coefficient`, where
16+
:math:`dist` is an amount, which describes a distance from the current pixel to border of the ellipse;
17+
:math:`coefficient` is a number which cuts a part of the channel's intensity of the **src** image (value is in the range :math:`[0, 1]`). New value stores to the **dst** image.
1718

18-
#. For every channel calculate :math:`dist`, :math:`radius\_ellipse` and :math:`coefficient`, where
19-
:math:`dist` is a distance between the current pixel and the center of image;
20-
:math:`radius\_ellipse` is a radius of ellipse in the this point which belongs the same line as a pixel;
21-
:math:`coefficient` is a number which multiplies the intensity channel.
19+
The :math:`coefficient` is calculated by the following formula:
2220

23-
The :math:`coefficient` is calculated by the following formula:
21+
.. math::
2422
25-
.. math::
23+
coefficient = 1 - ((dist - 1) / radiusMax),
2624
27-
coefficient = 1 - ((dist - radius\_ellipse) / (max\_radius - radius\_ellipse)),
28-
29-
where :math:`max\_radius` is a distance between the pixel (0, img_src.cols) and the center image.
30-
31-
#. Convert image to a BGR format.
25+
where :math:`radiusMax` is a maximum distance from border of the ellipse to the farthest pixel (a corner of the image).
3226

3327
Example.
3428

modules/photoeffects/include/opencv2/photoeffects.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ CV_EXPORTS_W void boostColor(cv::InputArray src, cv::OutputArray dst, float int
2424

2525
CV_EXPORTS_W int antique(cv::InputArray src, cv::OutputArray dst, cv::InputArray texture, float alpha);
2626

27-
CV_EXPORTS_W int vignette(cv::InputArray src, cv::OutputArray dst, cv::Size rect);
27+
CV_EXPORTS_W void vignette(cv::InputArray src, cv::OutputArray dst, cv::Size rect);
2828

2929
CV_EXPORTS_W int warmify(cv::InputArray src, cv::OutputArray dst, uchar delta = 30);
3030

modules/photoeffects/src/vignette.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class VignetteInvoker: public cv::ParallelLoopBody
1313
centerCol = imgSrc.cols / 2.0f;
1414
aSquare = rect.height * rect.height / 4.0f;
1515
bSquare = rect.width * rect.width / 4.0f;
16-
radiusMax = centerRow * centerRow / aSquare + centerCol * centerCol / bSquare - 1.0f;
16+
distMax = centerRow * centerRow / aSquare + centerCol * centerCol / bSquare - 1.0f;
1717
}
1818

1919
void operator()(const Range& rows) const
@@ -32,7 +32,7 @@ class VignetteInvoker: public cv::ParallelLoopBody
3232
float coefficient = 1.0f;
3333
if (dist > 1.0f)
3434
{
35-
coefficient = 1.0f - (dist - 1.0f) / radiusMax;
35+
coefficient = 1.0f - (dist - 1.0f) / distMax;
3636
}
3737
dstRow[3 * j] *= coefficient;
3838
dstRow[3 * j + 1] *= coefficient;
@@ -44,12 +44,12 @@ class VignetteInvoker: public cv::ParallelLoopBody
4444
private:
4545
const Mat& imgSrc;
4646
Mat& imgDst;
47-
float centerRow, centerCol, aSquare, bSquare, radiusMax;
47+
float centerRow, centerCol, aSquare, bSquare, distMax;
4848

4949
VignetteInvoker& operator=(const VignetteInvoker&);
5050
};
5151

52-
int vignette(InputArray src, OutputArray dst, Size rect)
52+
void vignette(InputArray src, OutputArray dst, Size rect)
5353
{
5454
CV_Assert(src.type() == CV_8UC3 && rect.height != 0 && rect.width != 0);
5555

@@ -60,8 +60,6 @@ int vignette(InputArray src, OutputArray dst, Size rect)
6060
Mat imgDst = dst.getMat();
6161

6262
parallel_for_(Range(0, imgSrc.rows), VignetteInvoker(imgSrc, imgDst, rect));
63-
64-
return 0;
6563
}
6664

67-
}}
65+
}}

modules/photoeffects/test/vignette_test.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ using namespace cv::photoeffects;
55

66
using namespace std;
77

8-
TEST(photoeffects_vignette, invalid_arguments)
8+
TEST(photoeffects_vignette, incorrect_image)
99
{
1010
Mat image(100, 100, CV_8UC1);
1111
Mat dst;
1212
Size rectangle;
13-
rectangle.height = 0;
14-
rectangle.width = 0;
13+
rectangle.height = image.rows / 1.5f;
14+
rectangle.width = image.cols / 2.0f;
1515

1616
EXPECT_ERROR(CV_StsAssert, vignette(image, dst, rectangle));
1717
}
1818

19-
TEST(photoeffects_vignette, test)
19+
TEST(photoeffects_vignette, incorrect_ellipse_size)
2020
{
2121
Mat image(100, 100, CV_8UC3);
2222
Mat dst;
2323
Size rectangle;
24-
rectangle.height = image.rows / 1.5f;
25-
rectangle.width = image.cols / 2.0f;
24+
rectangle.height = 0.0f;
25+
rectangle.width = 0.0f;
2626

27-
EXPECT_EQ(0, vignette(image, dst, rectangle));
27+
EXPECT_ERROR(CV_StsAssert, vignette(image, dst, rectangle));
2828
}
2929

3030
TEST(photoeffects_vignette, regression)
@@ -45,11 +45,11 @@ TEST(photoeffects_vignette, regression)
4545
FAIL() << "Can't read " + expectedOutput + " image";
4646
}
4747

48-
Size rect;
49-
rect.height = image.rows / 1.5f;
50-
rect.width = image.cols / 2.0f;
48+
Size rectangle;
49+
rectangle.height = image.rows / 1.5f;
50+
rectangle.width = image.cols / 2.0f;
5151

52-
EXPECT_EQ(0, vignette(image, dst, rect));
52+
vignette(image, dst, rectangle);
5353

5454
Mat diff = abs(rightDst - dst);
5555
Mat mask = diff.reshape(1) > 1;

0 commit comments

Comments
 (0)