2017-11-27 1 views
0

사진의 검은 색을 흰색으로, 그 반대로 바꾸려고합니다. 이것은 실제로 내 OCR 코드가 흰색 배경에서 더 잘 읽을 수 있도록합니다. 그것은 현재 내가있는 theyre 실제 비트 맵 작업 다른 질문을 본 적이 이미지 색상 바꾸기

Image img = Clipboard.GetImage(); 
      pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; 
      pictureBox1.Image = img; 

클립 보드

에서 이미지를 얻고,하지만 어떻게 내가이 클립 보드에서 직접 접근합니까?

+0

이 경우

// bmp is your bitmap var inverted = (255 - bitmap.ToMat()).ToMat(). .ToBitamp() // or .ToWriteableBitmap() // or .ToBitmapSource() 

OpenCV의 약간 과잉 될 수 있습니다 들어오는 이미지 그대로 문자 그대로 흑백이나 그레이 또는 기타 (미묘한) 색상이 포함될 수 있습니까? 그렇다면 어떻게 처리할까요? –

+0

이 흑백 이미지입니까? –

+0

어, 나는 이미지가 순수한 흑백이라고 의심합니다. 그 이유는 컬러 중 하나를 교체하면 빈 이미지가 생성되기 때문입니다. – Nyerguds

답변

2

ColorMatrix를 사용하여 다른 솔루션을.

ImageAttributes.SetColorMatrix를 수정하여 이미지 색상을 변경할 수 있습니다.
임계 값을 조정하여 밝기를 미세 조정합니다. 1.

로 설정하면

ImageAttributes.SetThreshold

설정하면 0으로 촬영 된 이미지는 또한 "반전"원본 비트 맵의 ​​색상 패턴에 의존한다는 생각 때문에 다른 접근을 시도, 모두 흰색 반대입니다 . 때때로 픽셀의 밝기를 반전시키지 않으면 더 나은 해결책을 얻을 수 있습니다.

OCR은 어떤 값이 더 적합한 지 확인하려면 "훈련"해야합니다. 에 R = 레드 G = 녹색 B = 블루 A = 알파 채널 W = 흰색 (밝기)
수정 밝기 구성 요소 :
Recoloring (MSDN)
ASCII Art Generator (CodeProject)

밝기 매트릭스 :

다음에서보세요 파일에서 이미지를 가져 오기에 "반전"

R G B A W 
R [1 0 0 0 0] 
G [0 1 0 0 0] 
B [0 0 1 0 0] 
A [0 0 0 1 0] 
W [b b b 0 1] <= Brightness 

를 얻을

,691,363,210
string _path = @"Image Path"; 
FileStream _fs = new FileStream(_path, FileMode.Open, FileAccess.Read); 
Image _img = new Bitmap(_fs); 

또는 클립 보드

Image _img = Clipboard.GetImage(); 


은 이와 같이 그래픽 콘텍스트를 생성하는 한계가 있다고해야한다. 이미지가 B & W 인 경우

using System.Drawing; 
using System.Drawing.Imaging; 


var BW_Inverted_matrix = new float[][] 
{ 
    new float[] { -1, -1, -1, 0, 0}, 
    new float[] { -1, -1, -1, 0, 0}, 
    new float[] { -1, -1, -1, 0, 0}, 
    new float[] { 0, 0, 0, 1, 0}, 
    new float[] { 1.5f, 1.5f, 1.5f, 0, 1} //Adjusted: to combine with threshold 
}; 
var BW_matrix = new float[][] 
{ 
    new float[] { 1, 1, 1, 0, 0}, 
    new float[] { 1, 1, 1, 0, 0}, 
    new float[] { 1, 1, 1, 0, 0}, 
    new float[] { 0, 0, 0, 1, 0}, 
    new float[] {-1, -1, -1, 0, 1} 
}; 

using (Graphics gr = Graphics.FromImage(_img)) 
{ 
    // Adjust the threshold as needed 
    float _threshold = 0.2f; 
    ImageAttributes _ImageAttributes = new ImageAttributes(); 
    _ImageAttributes.SetColorMatrix(new ColorMatrix(BW_Inverted_matrix)); 
    _ImageAttributes.SetThreshold(_threshold); 
    Rectangle _rect = new Rectangle(0, 0, _img.Width, _img.Height); 
    gr.DrawImage(_img, _rect, 0, 0, _img.Width, _img.Height, GraphicsUnit.Pixel, _ImageAttributes); 
} 
0

당신은 직접 이미지의 픽셀을 대체 할 System.Drawimg.Imaging 네임 스페이스에서 ColorMapImageAttributes 클래스를 사용할 수 있습니다

Image img = Clipboard.GetImage(); 
if (img != null) { 
    ColorMap[] cm = new ColorMap[1]; 
    cm[0] = new ColorMap(); 
    cm[0].OldColor = Color.Black; 
    cm[0].NewColor = Color.White; 
    ImageAttributes ia = new ImageAttributes(); 
    ia.SetRemapTable(cm); 
    using (Graphics g = Graphics.FromImage(img)) { 
    g.DrawImage(img, new Rectangle(Point.Empty, img.Size), 0, 0, img.Width, img.Height, 
       GraphicsUnit.Pixel, ia); 
    } 
    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; 
    pictureBox1.Image = img; 
} 
+0

이전'pictureBox1' 이미지를 삭제할 가치가 있습니까? 이 코드는 흰색에서 검정으로 매핑됩니까? – mjwills

+0

@mjwills 예. 흰색을 검정색으로 전환하려면 OldColor 및 NewColor 값을 변경하십시오. – LarsTech

0

, 그것은 OpenCV에 매우 쉽습니다 :이 전체 앱에서 유일한 조작에