投影轮廓法
在图像处理中,投影轮廓是指从二维图像沿轴的命中/正数之和的投影。投影轮廓法主要用于分割文本文档中存在的文本对象。
解决方案:
Note: Projection profile is calculated for a thresholded image or binarized image where a thresholded image is a grayscale image with pixel values as 0 or 255. Image pixels are replaced by 1 and 0 for pixel values 0 and 255 respectively.
投影轮廓针对不同的轴单独计算。沿垂直轴的投影轮廓称为垂直投影轮廓。将每一列的垂直投影轮廓计算为列内所有行像素值的总和。水平投影轮廓是图像沿水平轴的投影轮廓。将每一行的水平投影轮廓计算为行内所有列像素值的总和。
水平投影轮廓的代码实现:
C++
#include
using namespace std;
// Function to generate horizontal projection profile
vector getHorizontalProjectionProfile(
vector > image, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
// Convert black spots to ones
if (image[i][j] == 0)
{
image[i][j] = 1;
}
// Convert white spots to zeros
else if (image[i][j] == 255)
{
image[i][j] = 0;
}
}
}
vector horizontal_projection(rows, 0);
// Calculate sum of 1's for every row
for (int i = 0; i < rows; i++)
{
// Sum all 1's
for (int j = 0; j < cols; j++)
{
horizontal_projection[i] += image[i][j];
}
}
return horizontal_projection;
}
// Driver Function
int main()
{
int rows = 5, cols = 3;
vector > image = { { 0, 0, 0 },
{ 0, 255, 255 },
{ 0, 0, 0 },
{ 0, 255, 255 },
{ 0, 0, 0 }
};
vector horizontal_projection = getHorizontalProjectionProfile(
image, rows, cols);
for (auto it : horizontal_projection)
{
cout << it << " ";
}
return 0;
}
Python3
import numpy as np
# Function to generate horizontal projection profile
def getHorizontalProjectionProfile(image):
# Convert black spots to ones
image[image == 0] = 1
# Convert white spots to zeros
image[image == 255] = 0
horizontal_projection = np.sum(image, axis = 1)
return horizontal_projection
# Driver Function
if __name__ == '__main__':
rows = 5
cols = 3
image = np.array([[0, 0, 0],
[0, 255, 255],
[0, 0, 0],
[0, 255, 255],
[0, 0, 0]])
horizontal_projection = getHorizontalProjectionProfile(image.copy())
print(*horizontal_projection)
C++
#include
using namespace std;
// Function to generate vertical projection profile
vector getVerticalProjectionProfile(
vector > image, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
// Convert black spots to ones
if (image[i][j] == 0)
{
image[i][j] = 1;
}
// Convert white spots to zeros
else if (image[i][j] == 255)
{
image[i][j] = 0;
}
}
}
vector vertical_projection(cols, 0);
// Calculate sum of 1's for every column
for (int j = 0; j < cols; j++)
{
// Sum all 1's
for (int i = 0; i < rows; i++)
{
vertical_projection[j] += image[i][j];
}
}
return vertical_projection;
}
// Driver Function
int main()
{
int rows = 5, cols = 3;
vector > image = { { 0, 0, 0 },
{ 0, 255, 255 },
{ 0, 0, 0 },
{ 0, 255, 255 },
{ 0, 0, 0 }
};
vector vertical_projection = getVerticalProjectionProfile(
image, rows, cols);
for (auto it : vertical_projection)
{
cout << it << " ";
}
return 0;
}
Python3
import numpy as np
# Function to generate vertical projection profile
def getVerticalProjectionProfile(image):
# Convert black spots to ones
image[image == 0] = 1
# Convert white spots to zeros
image[image == 255] = 0
vertical_projection = np.sum(image, axis = 0)
return vertical_projection
# Driver Function
if __name__ == '__main__':
rows = 5
cols = 3
image = np.array([[0, 0, 0],
[0, 255, 255],
[0, 0, 0],
[0, 255, 255],
[0, 0, 0]])
vertical_projection = getVerticalProjectionProfile(image.copy())
print(*vertical_projection)
输出:
3 1 3 1 3
垂直投影轮廓的代码实现:
C++
#include
using namespace std;
// Function to generate vertical projection profile
vector getVerticalProjectionProfile(
vector > image, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
// Convert black spots to ones
if (image[i][j] == 0)
{
image[i][j] = 1;
}
// Convert white spots to zeros
else if (image[i][j] == 255)
{
image[i][j] = 0;
}
}
}
vector vertical_projection(cols, 0);
// Calculate sum of 1's for every column
for (int j = 0; j < cols; j++)
{
// Sum all 1's
for (int i = 0; i < rows; i++)
{
vertical_projection[j] += image[i][j];
}
}
return vertical_projection;
}
// Driver Function
int main()
{
int rows = 5, cols = 3;
vector > image = { { 0, 0, 0 },
{ 0, 255, 255 },
{ 0, 0, 0 },
{ 0, 255, 255 },
{ 0, 0, 0 }
};
vector vertical_projection = getVerticalProjectionProfile(
image, rows, cols);
for (auto it : vertical_projection)
{
cout << it << " ";
}
return 0;
}
Python3
import numpy as np
# Function to generate vertical projection profile
def getVerticalProjectionProfile(image):
# Convert black spots to ones
image[image == 0] = 1
# Convert white spots to zeros
image[image == 255] = 0
vertical_projection = np.sum(image, axis = 0)
return vertical_projection
# Driver Function
if __name__ == '__main__':
rows = 5
cols = 3
image = np.array([[0, 0, 0],
[0, 255, 255],
[0, 0, 0],
[0, 255, 255],
[0, 0, 0]])
vertical_projection = getVerticalProjectionProfile(image.copy())
print(*vertical_projection)
输出:
5 3 3
时间复杂度:O(行*列)
空间复杂度:O(行*列)