-1
Q
從圖像中刪除行
A
回答
2
這是一個有趣的問題,所以我給它一個鏡頭。下面我會告訴你如何提取和刪除水平和垂直線。你可以從中推斷出來。另外,爲了節省時間,我沒有預處理圖像,以便像應該那樣突出背景,這是一個改進的途徑。
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int, char** argv)
{
// Load the image
Mat src = imread(argv[1]);
// Check if image is loaded fine
if(!src.data)
cerr << "Problem loading image!!!" << endl;
Mat gray;
if (src.channels() == 3)
{
cvtColor(src, gray, CV_BGR2GRAY);
}
else
{
gray = src;
}
//inverse binary img
Mat bw;
//this will hold the result, image to be passed to OCR
Mat fin;
//I find OTSU binarization best for text.
//Would perform better if background had been cropped out
threshold(gray, bw, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
threshold(gray, fin, 0, 255, THRESH_BINARY | THRESH_OTSU);
imshow("binary", bw);
Mat dst;
Canny(fin, dst, 50, 200, 3);
Mat str = getStructuringElement(MORPH_RECT, Size(3,3));
dilate(dst, dst, str, Point(-1, -1), 3);
imshow("dilated_canny", dst);
//bitwise_and w/ canny image helps w/ background noise
bitwise_and(bw, dst, dst);
imshow("and", dst);
Mat horizontal = dst.clone();
Mat vertical = dst.clone();
fin = ~dst;
//Image that will be horizontal lines
Mat horizontal = bw.clone();
//Selected this value arbitrarily
int horizontalsize = horizontal.cols/30;
Mat horizontalStructure = getStructuringElement(MORPH_RECT, Size(horizontalsize,1));
erode(horizontal, horizontal, horizontalStructure, Point(-1, -1));
dilate(horizontal, horizontal, horizontalStructure, Point(-1, -1), 1);
imshow("horizontal_lines", horizontal);
//Need to find horizontal contours, so as to not damage letters
vector<Vec4i> hierarchy;
vector<vector<Point> >contours;
findContours(horizontal, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
for (const auto& c : contours)
{
Rect r = boundingRect(c);
float percentage_height = (float)r.height/(float)src.rows;
float percentage_width = (float)r.width/(float)src.cols;
//These exclude contours that probably are not dividing lines
if (percentage_height > 0.05)
continue;
if (percentage_width < 0.50)
continue;
//fills in line with white rectange
rectangle(fin, r, Scalar(255,255,255), CV_FILLED);
}
int verticalsize = vertical.rows/30;
Mat verticalStructure = getStructuringElement(MORPH_RECT, Size(1,verticalsize));
erode(vertical, vertical, verticalStructure, Point(-1, -1));
dilate(vertical, vertical, verticalStructure, Point(-1, -1), 1);
imshow("verticalal", vertical);
findContours(vertical, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
for (const auto& c : contours)
{
Rect r = boundingRect(c);
float percentage_height = (float)r.height/(float)src.rows;
float percentage_width = (float)r.width/(float)src.cols;
//These exclude contours that probably are not dividing lines
if (percentage_width > 0.05)
continue;
if (percentage_height < 0.50)
continue;
//fills in line with white rectange
rectangle(fin, r, Scalar(255,255,255), CV_FILLED);
}
imshow("Result", fin);
waitKey(0);
return 0;
}
這種方法的侷限性是,該線需要是直的。由於底線的曲線,它在「能量」中略微削減爲「E」。也許像建議的那樣(我從來沒有使用過)檢測到hough線,可以設計出類似但更穩健的方法。另外,用矩形填充線條可能不是最好的方法。
相關問題
- 1. 從Excel中刪除行和圖像
- 2. 從MediaRouteControllerDialog中刪除圖像
- 3. 從UI中刪除圖像
- 4. 從NSMutableArray中刪除圖像
- 5. 從Gridview中刪除圖像
- 6. 從UICollectionViewController中刪除圖像
- 7. 從UIImageView中刪除圖像
- 8. 從IKImageView中刪除圖像
- 9. 從圖像中刪除卷
- 10. 從文件中刪除圖像,同時刪除mysql行
- 11. 從swift中刪除圖像像素
- 12. 從ImageView刪除/刪除當前圖像?
- 13. 從dhtmlxTree視圖中刪除圖像
- 14. 如何從圖形中刪除圖像
- 15. 試圖從Xcode 4中刪除圖像
- 16. 圖像處理刪除行
- 17. 從UIWebview刪除圖像
- 18. 從PHP中的圖像中刪除URL
- 19. 在codeigniter中刪除後從文件夾中刪除圖像
- 20. 圖像,從存儲中刪除,也
- 21. 如何從圖像中刪除文本
- 22. 從圖像中刪除顏色
- 23. 用C++從文件中刪除圖像
- 24. 從圖像中刪除對象
- 25. 從圖像中刪除邊框
- 26. 從緩存中刪除特定圖像?
- 27. Java從面板中刪除圖像
- 28. Matlab:從圖像中刪除白細胞
- 29. 從陣列中刪除圖像
- 30. 從ActiveX控件中刪除圖像-VBA
而不是將線看作是一個「障礙物」,你有沒有嘗試將它們當作輪廓或使用邊緣檢測器來傳遞線條形成的矩形內容?例如。 「營養信息...」將是一個盒子,而宏觀營養素分解將是另一個盒子 –
@TrésDuBiel是的,我試過了,但一些營養素事實表中有營養素和它的價值之間的垂直線,如脂肪| 2.7g,創建障礙物之間的垂直線 –
對於線條檢測,您可以使用[hough lines](http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html) OpenCV的。 – seleciii44