How to remove horizontal line from this x-ray image? - c++

I'm new in opencv (c++) and I want to remove horizontal line from this x-ray image. But I can not.
These is my image:
What ideas on how to solve this task would you suggest? Or on what resource on the internet can I find help?
This is my c++ code
src = imread("C:/Users/Alireza/Desktop/New folder (3)/11.bmp");
cvtColor(src, gray, CV_RGB2GRAY);
imshow("Original Image", gray);
imwrite("Original Image.png", gray);
normalize(gray, gray, 0, 250, NORM_MINMAX, -1, Mat());
threshold(gray, thresh, 170, 255, THRESH_BINARY_INV);
vector< vector <Point> > contours;
vector< Vec4i > hierarchy;
int largest_contour_index = 0;
int largest_area = 0;
Mat alpha(src.size(), CV_8UC1, Scalar(0));
findContours(thresh, contours, hierarchy, CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
for (int i = 0; i< contours.size(); i++)
{
double a = contourArea(contours[i], false);
if (a>largest_area)
{
largest_area = a;
largest_contour_index = i;
}
}
drawContours(alpha, contours, largest_contour_index,Scalar(255),CV_FILLED, 8, hierarchy);
vector<Mat> rgb;
split(src, rgb);
Mat rgba[4] = { rgb[0], rgb[1], rgb[2], alpha };
merge(rgba, 4, Tafrigh);
imshow("Tafrigh", Tafrigh);
imwrite("Tafrigh.png", Tafrigh);

Take a 2D FFT, take a look at the spectrum. You will see plenty of dots along the center y-axis. Suppress those dots , back transform, your vertical lines will be gone.
Below the result (since I don't have C++ and opencv installed) in Python, with sliders to vary the region to be suppressed. Consider it Pseudocode. This is still pretty rough as I'm not making a smooth transition between suppressed pixels and their neighbors here for simplicity.
%matplotlib inline
from __future__ import division
import numpy as np
import matplotlib.pyplot as p
from ipywidgets import *
from scipy import misc
f = misc.imread('xray_image_with_horizontal_lines.png')
a=np.fft.fftshift(np.fft.fft2(f))
def process(kx,ky):
p.figure(figsize=(12,8))
p.subplot(221)
p.imshow(f, cmap=p.cm.gray)
p.subplot(222)
p.imshow(np.abs(np.log(a)), cmap=p.cm.gray)
print np.shape(a)
b=np.zeros_like(a)
for i in range(639):
for j in range(406):
if not ( 320-kx<i<320+kx and (j<203-ky or j>203+ky)):
b[j,i]=a[j,i]
c=np.fft.ifft2(b)
p.subplot(223)
p.imshow(np.abs(np.log(b)), cmap=p.cm.gray)
p.subplot(224)
p.imshow(np.abs(c), cmap=p.cm.gray)
interact(process, kx=[1,20,1],ky=[1,20,1])

That's just an idea : keep mean line constant.
cv::Mat image = cv::imread("Tf6HO.png",CV_LOAD_IMAGE_GRAYSCALE);
vector<double> moyenne;
double minval,maxval;
minMaxLoc(image,&minval,&maxval);
imshow("original",image);
for (int i = 0; i < image.rows; i++)
{
double s=0;
// Caluclate mean for row i
for (int j=0;j<image.cols;j++)
s += image.at<uchar>(i,j);
// Store result in vector moyenne
moyenne.push_back(s/image.cols);
}
// Energy for row i equal to a weighted mean of row in [i-nbInf,i+nbSup]
int nbInf=32,nbSup=0;
for (int i = 0; i < image.rows; i++)
{
double s=0,p=0;
// weighted mean (border effect process with max and min method
for (int j = max(0, i - nbInf); j <= min(image.rows - 1, i + nbSup); j++)
{
s+=moyenne[j]*1./(1+abs(i-j));
p+=1./(1+abs(i-j));
}
// Weighted mean
s/=p;
// process pixel in row i : mean of row i equal to s
for (int j=0;j<image.cols;j++)
image.at<uchar>(i,j) =saturate_cast<uchar>((image.at<uchar>(i,j)-moyenne[i])+s);
}
imshow("process",image);
waitKey();
or with reduce resolution
if you want to improve you can read this paper and bibliography

Related

Removing a black part of image after stitching 2 images in OpenCV C++

So I stitched 2 images in OpenCV C++ but I know have a full black part in the image and would like to remove it. What would be the way to go?
Here is my image output:
The idea is to sum the pixels of each column then iterate through the data to construct the new image. If the value of a column is zero then it means it is black so we ignore it otherwise we concatenate the column ROI to the final image. Here's the summation of the column pixels:
Result
I implemented it in Python but you can adapt a similar idea to C++
import cv2
import numpy as np
# import matplotlib.pyplot as plt
# Load image, convert to grayscale, and sum column pixels
image = cv2.imread('1.jpg')
h, w = image.shape[:2]
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
first_pass = True
pixels = np.sum(gray, axis=0).tolist()
# Build new image
for index, value in enumerate(pixels):
if value == 0:
continue
else:
ROI = image[0:h, index:index+1]
if first_pass:
result = image[0:h, index+1:index+2]
first_pass = False
continue
result = np.concatenate((result, ROI), axis=1)
cv2.imshow('result', result)
cv2.imwrite('result.png', result)
# Uncomment for plot visualization
# plt.plot(pixels, color='teal')
# plt.show()
cv2.waitKey()
Note: According to nathancy's answer I just coded using C++:
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("/your/image/directory/image.jpg");
for(int i=0;i<img.cols;i++)
{
int black_cnt = 0;
for(int j=0;j<img.rows;j++)
{
if(img.at<cv::Vec3b>(j,i)[0]==0)
black_cnt++;
}
if(black_cnt==img.rows)
continue;
else
{
Rect roi(i,0,img.cols-i,img.rows);
img = img(roi);
break;
}
}
imshow("Result",img);
waitKey(0);
return 0;
}
Fast way to do it is to use cv::reduce function of OpenCv and find maximum value per column. It is faster than making sum of elements. If max value in column is 0, it means that column is black.
Input of cv::reduce is 2d-array:
[a b c]
[d e f]
[g h i]
as output will get matrix 2d with one row - vector.
[max(a,d,g) max(b,e,h) max(c,f,i)]
Then you need to find cutOff index - first non-black column, and extract ROI:
cv::Mat img = imread("test.jpg");
cv::Mat out;
cv::reduce(img, out, 0, cv::REDUCE_MAX);
int cutOffIdx = 0;
for (int col = 0; col < out.cols; ++col) {
const cv::Vec3b& vec = out.at<Vec3b>(0, col);
if (vec[0] || vec[1] || vec[2]) {
cutOffIdx = col;
break;
}
}
cv::imshow("test",img(cv::Rect(cutOffIdx,0,img.cols-cutOffIdx-1,img.rows)));
cv::waitKey(0);
I would do this:
Thresholding the graysclae image
Finding the outermost contours in the image
Find the biggest one from the contours
Get the bounding box of that contour
Crop the image by that bounding box
And the code (C++ opencv):
Mat K,J,I = imread("D:/1.jpg",1);
cvtColor(I, K, CV_BGR2GRAY);
threshold(K, J, 0, 255, THRESH_BINARY);
vector<vector<Point>> contours;
vector< Vec4i > hierarchy;
findContours(J, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE); # Gives the outer contours
Mat tmp = Mat::zeros(I.size(), CV_8U);
int k = 0;
double max = -1;
for (size_t i = 0; i < contours.size(); i++) # Of course in this case, There is only one external contour but I write the loop for more clarification
{
double area = contourArea(contours[i]);
if (area > max)
{
k = i;
max = area;
}
}
drawContours(tmp, contours, k, Scalar(255, 255, 255), -1); # You can comment this line. I wrote it just for showing the procedure
Rect r = cv::boundingRect(contours[k]);
Mat output;
I(r).copyTo(output);
imshow("0", I);
imshow("1", J);
imshow("2", tmp);
imshow("3", output);
waitKey(0);

OpenCV code doesn't work on specific image

I am trying to run the followin code (based on this page) on an image, but it doesn't work:
Mat src=imread("img.jpg",1);
Mat tmp,thr;
cvtColor(src,tmp,CV_BGR2GRAY);
threshold(tmp,thr,200,255,THRESH_BINARY_INV);
vector< vector <Point> > contours;
vector< Vec4i > hierarchy;
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0));//Ceate Mat to draw contour
int box_w=10; // Define box width here
int box_h=10; // Define box height here
int threshold_perc=25; //perceantage value for eliminating the box according to pixel count inside the box
int threshold=(box_w*box_h*threshold_perc)/100;
findContours( thr, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); //Find contour
for( int i = 0; i< contours.size(); i++ ){
drawContours( dst,contours, i, Scalar(255,255,255),CV_FILLED, 8, hierarchy ); // Draw contour with thickness = filled
Rect r= boundingRect(contours[i]); // Find bounding rect
// Scan the image with in bounding box
for(int j=r.x;j<r.x+r.width;j=j+box_w){
for(int k=r.y;k<r.y+r.height;k=k+box_h){
Rect roi_rect(j,k,box_w,box_h);
Mat roi = dst(roi_rect);
int count = countNonZero(roi);
if(count > threshold)
rectangle(src, roi_rect, Scalar(255,0,0),1,8,0 );
}
}
}
imshow("src",src);
waitKey();
It works fine for any normal image, but for the images below, it either breaks or doesn't find the contour and draws boxes all over the image.
It says:
Unhandled exception at 0x00007FF9A72DA388 in test2.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000FECC9DEAC0.
It breaks and points to here:
inline
Mat Mat::operator()( const Rect& roi ) const
{
return Mat(*this, roi);
}
in mat.inl.hpp.
What is wrong with my image? I have changed it from Gray-scale to RGB, but didn't help.
On the following image, it works fine:
As I commented, you're trying to access a region of the image that doesn't exist by using a rectangle of fixed size.
By intersecting the roi with the rectangle, you can avoid this problem:
Mat roi = dst(roi_rect & r);
The problem was that in the first images, the contour gets close to the boundaries of the image and in the bottom for loop of the program, it exceeds the coordinates. It was fixed with this:
// Scan the image with in bounding box
for (int j = r.x;j<r.x + r.width;j = j + box_w) {
for (int k = r.y;k<r.y + r.height;k = k + box_h) {
Rect roi_rect(j, k, box_w, box_h);
if (j + box_w < dst.cols && k + box_h < dst.rows)
{
Mat roi = dst(roi_rect);
int count = countNonZero(roi);
if (count > threshold)
rectangle(src, roi_rect, Scalar(0,0,255), 1, 8, 0);
}
}
}

Get Pixel Values along several lines from a mask which is tilted

currently I have segmented the object (rectangular) and now I want to create a Line profile. I dont know how to get along this line.
detected object
Aim is to get this:
object with lines
Update 14:25:
I know already the angle from the bounding rect and used this to calculate the shift in y-direction in order to rearrange the values to a new mat so that I only need go through the matrix to get a line profile.
Here my Code, but the rearrangement did not work.
Mat imgIn(SizeY, SizeX, CV_16U, &Wire[0]),
imgOut(SizeY, SizeX, CV_16U, Scalar(0)),
temp, drawing, mask, lineProfile(SizeY, SizeX, CV_16U, Scalar(0));
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
Point center;
char buffer[100];
bool found = false;
int rect_no (0);
double angle(0.0);
// Detecting outer contours
temp = ::adaptiveThreshold(imgIn, SizeY, SizeX, kernelSize, thresh, 0);
// Find contours
findContours(temp, contours, hierarchy, CV_RETR_TREE, CHAIN_APPROX_TC89_KCOS, Point(0, 0) );
/// Find the rotated rectangles and ellipses for each contour
vector<RotatedRect> minRect( contours.size() );
for( int i = 0; i < contours.size(); i++ ) minRect[i] = minAreaRect( Mat(contours[i]) );
// Draw contours + rotated rects
drawing = Mat::zeros(temp.size(), CV_8U );
Point2f rect_points[4];
for( int i = 0; i< minRect.size(); i++ ){
if((float)minRect[i].boundingRect().height/(float)minRect[i].boundingRect().width > 3.0 && (float)minRect[i].boundingRect().height/(float)minRect[i].boundingRect().width < 4.9){
// rotated rectangle
minRect[i].points(rect_points);
for(int j = 0; j < 4; j++) line(drawing, rect_points[j], rect_points[(j+1)%4], Scalar(255), 1, 8);
//found = minRect[i].boundingRect().contains(Point(459, 512));
if(minRect[i].boundingRect().area() > 1000)
rect_no = i;
}
}
center = computeCentroid(drawing);
cv::floodFill(drawing, center, cv::Scalar(255));
drawing.convertTo(imgOut, CV_16U, 257.0);
imgIn.copyTo(imgOut, drawing);
// Calculate Wire SR_min
// Get angle of Wire
angle = (90 - (-1 * minRect[rect_no].angle))*(CV_PI/180);
for(int i = 0;i < SizeY;i++){
for(int j = 0;j < SizeX;j++){
if(imgOut.at<ushort>(i, j) != 0)
lineProfile.at<ushort>(i, j) = imgOut.at<ushort>((unsigned short)(i/cos(angle)), j);
}
}
for(int i = 0;i < SizeY;i++){
for(int j = 0;j < SizeX;j++){
*Wire++ = lineProfile.at<ushort>(i, j);//imgOut.at<ushort>(i, j);
}
}
If you know the coordinates of the beginning and the end of your line, getting the values at each point on the line should be easy with OpenCV's LineIterator. Feed it your image and your two points and let it work its magic.
If you are able to binarize the detected object image , then you could possibly able to apply Houghlines function of OpenCv . You can find it in the below link
http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html

How to run findContours() on meanShiftSegmentation() output?

I'm trying to rewrite my very slow naive segmentation using floodFill to something faster. I ruled out meanShiftFiltering a year ago because of the difficulty in labelling the colours and then finding their contours.
The current version of opencv seems to have a fast new function that labels segments using mean shift: gpu::meanShiftSegmentation(). It produces images like the following:
(source: ekran.org)
So this looks to me pretty close to being able to generating contours. How can I run findContours to generate segments?
Seems to me, this would be done by extracting the labelled colours from the image, and then testing which pixel values in the image match each label colour to make a boolean image suitable for findContours. This is what I have done in the following (but its a bit slow and strikes me there should be a better way):
Mat image = imread("test.png");
...
// gpu operations on image resulting in gpuOpen
...
// Mean shift
TermCriteria iterations = TermCriteria(CV_TERMCRIT_ITER, 2, 0);
gpu::meanShiftSegmentation(gpuOpen, segments, 10, 20, 300, iterations);
// convert to greyscale (HSV image)
vector<Mat> channels;
split(segments, channels);
// get labels from histogram of image.
int size = 256;
labels = Mat(256, 1, CV_32SC1);
calcHist(&channels.at(2), 1, 0, Mat(), labels, 1, &size, 0);
// Loop through hist bins
for (int i=0; i<256; i++) {
float count = labels.at<float>(i);
// Does this bin represent a label in the image?
if (count > 0) {
// find areas of the image that match this label and findContours on the result.
Mat label = Mat(channels.at(2).rows, channels.at(2).cols, CV_8UC1, Scalar::all(i)); // image filled with label colour.
Mat boolImage = (channels.at(2) == label); // which pixels in labeled image are identical to this label?
vector<vector<Point>> labelContours;
findContours(boolImage, labelContours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
// Loop through contours.
for (int idx = 0; idx < labelContours.size(); idx++) {
// get bounds for this contour.
bounds = boundingRect(labelContours[idx]);
// create ROI for bounds to extract this region
Mat patchROI = image(bounds);
Mat maskROI = boolImage(bounds);
}
}
}
Is this the best approach or is there a better way to get the label colours? Seems it would be logical for meanShiftSegmentation to provide this information? (vector of colour values, or vector of masks for each label, etc.)
Thank you.
Following is another way of doing this without thowing away the colour information in the meanShiftSegmentation results. I did not compare the two for performance.
// Loop through whole image, pixel and pixel and then use the colour to index an array of bools indicating presence.
vector<Scalar> colours;
vector<Scalar>::iterator colourIter;
vector< vector< vector<bool> > > colourSpace;
vector< vector< vector<bool> > >::iterator colourSpaceBIter;
vector< vector<bool> >::iterator colourSpaceGIter;
vector<bool>::iterator colourSpaceRIter;
// Initialize 3D Vector
colourSpace.resize(256);
for (int i = 0; i < 256; i++) {
colourSpace[i].resize(256);
for (int j = 0; j < 256; j++) {
colourSpace[i][j].resize(256);
}
}
// Loop through pixels in the image (should be fastish, look into LUT for faster)
uchar r, g, b;
for (int i = 0; i < segments.rows; i++)
{
Vec3b* pixel = segments.ptr<Vec3b>(i); // point to first pixel in row
for (int j = 0; j < segments.cols; j++)
{
b = pixel[j][0];
g = pixel[j][1];
r = pixel[j][2];
colourSpace[b][g][r] = true; // this colour is in the image.
//cout << "BGR: " << int(b) << " " << int(g) << " " << int(r) << endl;
}
}
// Get all the unique colours from colourSpace
// loop through colourSpace
int bi=0;
for (colourSpaceBIter = colourSpace.begin(); colourSpaceBIter != colourSpace.end(); colourSpaceBIter++) {
int gi=0;
for (colourSpaceGIter = colourSpaceBIter->begin(); colourSpaceGIter != colourSpaceBIter->end(); colourSpaceGIter++) {
int ri=0;
for (colourSpaceRIter = colourSpaceGIter->begin(); colourSpaceRIter != colourSpaceGIter->end(); colourSpaceRIter++) {
if (*colourSpaceRIter)
colours.push_back( Scalar(bi,gi,ri) );
ri++;
}
gi++;
}
bi++;
}
// For each colour
int segmentCount = 0;
for (colourIter = colours.begin(); colourIter != colours.end(); colourIter++) {
Mat label = Mat(segments.rows, segments.cols, CV_8UC3, *colourIter); // image filled with label colour.
Mat boolImage = Mat(segments.rows, segments.cols, CV_8UC3);
inRange(segments, *colourIter, *colourIter, boolImage); // which pixels in labeled image are identical to this label?
vector<vector<Point> > labelContours;
findContours(boolImage, labelContours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
// Loop through contours.
for (int idx = 0; idx < labelContours.size(); idx++) {
// get bounds for this contour.
Rect bounds = boundingRect(labelContours[idx]);
float area = contourArea(labelContours[idx]);
// Draw this contour on a new blank image
Mat maskImage = Mat::zeros(boolImage.rows, boolImage.cols, boolImage.type());
drawContours(maskImage, labelContours, idx, Scalar(255,255,255), CV_FILLED);
Mat patchROI = frame(bounds);
Mat maskROI = maskImage(bounds);
}
segmentCount++;
}

OpenCV Line Detection

I am trying to find the edges of the centered box in this image:
I have tried using a HoughLines using dRho=img_width/1000, dTheta=pi/180, and threshold=250
It works great on this image, scaled to 1/3 of the size, but on the full size image it just gets lines everywhere in every direction...
What can I do to tune this to be more accurate?
The code to achieve the result below is a slight modification of the one presented in this answer: how to detect a square:
The original program can be found inside OpenCV, it's called squares.cpp. The code below was modified to search squares only in the first color plane, but as it still detects many squares, at the end of the program I discard all of them except the first, and then call draw_squares() to show what was detected. You can change this easilly to draw all of them and see everything that was detected.
You can do all sorts of thing from now own, including setting a (ROI) region of interest to extract the area that's inside the square (ignore everything else around it).
You can see that the detected rectangle is not perfectly aligned with the lines in the image. You should perform some pre-processing (erode?) operations in the image to decrease the thickness of lines and improve the detection. But from here on it's all on you:
#include <cv.h>
#include <highgui.h>
using namespace cv;
double angle( cv::Point pt1, cv::Point pt2, cv::Point pt0 ) {
double dx1 = pt1.x - pt0.x;
double dy1 = pt1.y - pt0.y;
double dx2 = pt2.x - pt0.x;
double dy2 = pt2.y - pt0.y;
return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}
void find_squares(Mat& image, vector<vector<Point> >& squares)
{
// TODO: pre-processing
// blur will enhance edge detection
Mat blurred(image);
medianBlur(image, blurred, 9);
Mat gray0(blurred.size(), CV_8U), gray;
vector<vector<Point> > contours;
// find squares in the first color plane.
for (int c = 0; c < 1; c++)
{
int ch[] = {c, 0};
mixChannels(&blurred, 1, &gray0, 1, ch, 1);
// try several threshold levels
const int threshold_level = 2;
for (int l = 0; l < threshold_level; l++)
{
// Use Canny instead of zero threshold level!
// Canny helps to catch squares with gradient shading
if (l == 0)
{
Canny(gray0, gray, 10, 20, 3); //
// Dilate helps to remove potential holes between edge segments
dilate(gray, gray, Mat(), Point(-1,-1));
}
else
{
gray = gray0 >= (l+1) * 255 / threshold_level;
}
// Find contours and store them in a list
findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
// Test contours
vector<Point> approx;
for (size_t i = 0; i < contours.size(); i++)
{
// approximate contour with accuracy proportional
// to the contour perimeter
approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);
// Note: absolute value of an area is used because
// area may be positive or negative - in accordance with the
// contour orientation
if (approx.size() == 4 &&
fabs(contourArea(Mat(approx))) > 1000 &&
isContourConvex(Mat(approx)))
{
double maxCosine = 0;
for (int j = 2; j < 5; j++)
{
double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
maxCosine = MAX(maxCosine, cosine);
}
if (maxCosine < 0.3)
squares.push_back(approx);
}
}
}
}
}
void draw_squares(Mat& img, vector<vector<Point> > squares)
{
for (int i = 0; i < squares.size(); i++)
{
for (int j = 0; j < squares[i].size(); j++)
{
cv::line(img, squares[i][j], squares[i][(j+1) % 4], cv::Scalar(0, 255, 0), 1, CV_AA);
}
}
}
int main(int argc, char* argv[])
{
Mat img = imread(argv[1]);
vector<vector<Point> > squares;
find_squares(img, squares);
std::cout << "* " << squares.size() << " squares were found." << std::endl;
// Ignore all the detected squares and draw just the first found
vector<vector<Point> > tmp;
if (squares.size() > 0)
{
tmp.push_back(squares[0]);
draw_squares(img, tmp);
}
//imshow("squares", img);
//cvWaitKey(0);
imwrite("out.png", img);
return 0;
}
when resizing the image, the image is normally first blurred with a filter, e.g. Gaussian, in order to get rid of high frequencies. The fact that resized one works better is likely because your original image is somehow noisy.
Try blur the image first, e.g. with cv::GaussianBlur(src, target, Size(0,0), 1.5), then it should be equivalent to resizing. (It forgot the theory, if it does not work, try 3 and 6 as well)
Try using a preprocessing pass with the erosion filter. It will give you the same effect as the downscaling - the lines will become thinner and will not disappear at the same time.
The "Blur" filter is also a good idea, as chaiy says.
This way (with blur) it will become something like http://www.ic.uff.br/~laffernandes/projects/kht/index.html (Kernel Based Hough Transform)