You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2025-01-20-shape-normalization.md
+21-32Lines changed: 21 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,16 +54,7 @@ Let’s have a look at some images we would like to normalize in shape. We want
54
54
</table>
55
55
56
56
The characters can be written in either white or black. To add some adaptability, we will threshold at 127 (considering 8-bit depth), rescale the image to a size of 512x512, and calculate the relative percentage of white pixels in the image. If this percentage is less than 0.5, we are dealing with an image where white pixels dominate, and therefore the character will be made white. Finally, we will scale the amplitude to the range [0-1]:
where $n$ is the total of rows, $m$ is the total of columns, $I(x, y) \in \{0, 1\}$ is the (binary, in this case) pixel intensity and $(x, y) \in [0, 512)^2$ is the pixel position.
80
+
where $$n$$ is the total of rows, $$m$$ is the total of columns, $$I(x, y) \in \{0, 1\}$$ is the (binary, in this case) pixel intensity and $$(x, y) \in [0, 512)^2$$ is the pixel position.
2. Now, we compute the shape dispersion matrix $M$ with the moments $m_{11}$ (second moment along the $x$-axis), $m_{22}$ (second moment along the $y$-axis), $m_{12}$ and $m_{21}$ (cross moments that represent correlations between $x$ and $y$):
100
+
2. Now, we compute the shape dispersion matrix $$M$$ with the moments $$m_{11}$$ (second moment along the $$x$$-axis), $$m_{22}$$ (second moment along the $$y$$-axis), $$m_{12}$$ and $$m_{21}$$ (cross moments that represent correlations between $$x$$ and $$y$$):
To do this, we perform the following transformations:
163
-
- Translate the origin to the centroid $(\bar{x}, \bar{y})$,
164
-
- Rotate the axes to align with the eigenvectors of $M$,
165
-
- Scale the axes according to the eigenvalues $\lambda_1$ and $\lambda_2$.
152
+
- Translate the origin to the centroid $$(\bar{x}, \bar{y})$$,
153
+
- Rotate the axes to align with the eigenvectors of $$M$$,
154
+
- Scale the axes according to the eigenvalues $$\lambda_1$$ and $$\lambda_2$$.
166
155
167
-
To ensure that the area of the transformed image approximately matches the area of the input image (which may be of interest), we introduce an adaptive scaling factor $k$ based on the area of the character:
168
-
```C++
156
+
To ensure that the area of the transformed image approximately matches the area of the input image (which may be of interest), we introduce an adaptive scaling factor $$k$$ based on the area of the character:
157
+
```cpp
169
158
const int whitePixelCountOriginal = cv::countNonZero(image);
170
159
171
160
cv::Mat normalized;
@@ -264,8 +253,8 @@ We will understand the above code:
264
253
***Error Calculation**:
265
254
After constructing the transformed image, the number of white pixels in the transformed image is compared (relative difference between areas) with the original number of white pixels.
266
255
267
-
***Adjustment of Scaling Factor $k$**:
268
-
If the transformed image's area is smaller than the original, $k$ is increased, otherwise $k$ is decreased. This brings transformed area closer to the original in each iteration.
256
+
***Adjustment of Scaling Factor $$k$$**:
257
+
If the transformed image's area is smaller than the original, $$k$$ is increased, otherwise $$k$$ is decreased. This brings transformed area closer to the original in each iteration.
269
258
270
259
***Stopping criteria**:
271
260
The algorithm terminates when the error between the original and transformed image areas is below the specified tolerance, or when the maximum number of iterations is reached.
0 commit comments