How to convert HSV to RGB in flutter - flutter

I'm using this code to convert RGB to HSV.
now i have to revert back to RGB.
is that any way to convert this in flutter??
I just recently started working in flutter, please help me out, I'm stuck here.
Thank you so much in advance.
rgbToHsv(int r1, int g1, int b1) {
// R, G, B values are divided by 255
// to change the range from 0..255 to 0..1:
double r = r1 / 255.0;
double g = g1 / 255.0;
double b = b1 / 255.0;
// h, s, v = hue, saturation, value
var cmax = [r, g, b].reduce(max); // maximum of r, g, b
var cmin = [r, g, b].reduce(min); // minimum of r, g, b
var diff = cmax - cmin; // diff of cmax and cmin.
var h;
var s;
var v;
//Get value of h
if (cmax == cmin) {
h = 0;
} else if (cmax == r) {
h = (60 * ((g - b) / diff) + 360) % 360;
} else if (cmax == g) {
h = (60 * ((b - r) / diff) + 120) % 360;
} else if (cmax == b) {
h = (60 * ((r - g) / diff) + 240) % 360;
}
//Get value of s
if (cmax == 0) {
s = 0;
} else {
s = (diff / cmax) * 100;
}
//Get value of v
v = cmax * 100;
//Convert HSV [360, 100, 100] to HSV [256, 256, 256]
double h_256 = (h / 360) * 255;
double s_256 = (s / 100) * 255;
double v_256 = (v / 100) * 255;
int h_256_int = h_256.toInt();
int s_256_int = s_256.toInt();
int v_256_int = v_256.toInt();
//Convert to HSV HEX
var hex_h = h_256_int.toRadixString(16);
var hex_s = s_256_int.toRadixString(16);
var hex_v = v_256_int.toRadixString(16);
//MERGE HSV HEX
var finalColor = hex_h + hex_s + hex_v;
print("HSV HEX:" + finalColor.toUpperCase());
/////RGB TRANS>>>>>>>>>>>>>>>>>
////////RGB TRANS>>>>>>>>>>>>>>>>>
////////RGB TRANS>>>>>>>>>>>>>>>>>
////////RGB TRANS>>>>>>>>>>>>>>>>>
var _h = hextToint(hex_h);
var _s = hextToint(hex_s);
var _v = hextToint(hex_v);
print(_h);
print(_s);
print(_v);
print(hsvToRgb(_h.toDouble(), _s.toDouble(), _v.toDouble()));
//return rgb;
}

this might help. I have used this in one of my projects.
String hsvToRgb(double H, double S, double V) {
int R, G, B;
H /= 360;
S /= 100;
V /= 100;
if (S == 0) {
R = (V * 255).toInt();
G = (V * 255).toInt();
B = (V * 255).toInt();
} else {
double var_h = H * 6;
if (var_h == 6) var_h = 0; // H must be < 1
int var_i = var_h.floor(); // Or ... var_i =
// floor( var_h )
double var_1 = V * (1 - S);
double var_2 = V (1 - S (var_h - var_i));
double var_3 = V (1 - S (1 - (var_h - var_i)));
double var_r;
double var_g;
double var_b;
if (var_i == 0) {
var_r = V;
var_g = var_3;
var_b = var_1;
} else if (var_i == 1) {
var_r = var_2;
var_g = V;
var_b = var_1;
} else if (var_i == 2) {
var_r = var_1;
var_g = V;
var_b = var_3;
} else if (var_i == 3) {
var_r = var_1;
var_g = var_2;
var_b = V;
} else if (var_i == 4) {
var_r = var_3;
var_g = var_1;
var_b = V;
} else {
var_r = V;
var_g = var_1;
var_b = var_2;
}
R = (var_r * 255).toInt(); // RGB results from 0 to 255
G = (var_g * 255).toInt();
B = (var_b * 255).toInt();
}
String rs = R.toRadixString(16);
String gs = G.toRadixString(16);
String bs = B.toRadixString(16);
if (rs.length == 1) rs = "0" + rs;
if (gs.length == 1) gs = "0" + gs;
if (bs.length == 1) bs = "0" + bs;
return "#" + rs + gs + bs;
}

RGB to HSV
HSVColor rgbToHSV(int r, int g, int b, {double opacity = 1}) {
return HSVColor.fromColor(Color.fromRGBO(r, g, b, opacity));
}
HSV to RGB
List<int> hsvToRGB(HSVColor color) {
//convert to color
final c = color.toColor();
return [c.red, c.blue, c.green];
}
To use HSVColor use myHSVcolor.toColor().
More about HSV Color.

Expanding on Yeasin Sheikh answer
Flutter
RGB to HSV
HSVColor rgbToHSV(int r, int g, int b, {double opacity = 1}) {
return HSVColor.fromColor(Color.fromRGBO(r, g, b, opacity));
}
HSV to RGB
List<int> hsvToRGB(HSVColor color) {
//convert to color
final c = color.toColor();
return [c.red, c.blue, c.green];
}
To use HSVColor use myHSVcolor.toColor().
More about HSV Color.
Native Dart
Import color package
RGB to HSV
final RgbColor rgbColor = RgbColor(red, green, blue);
final HsvColor hsvColor = rgbColor.toHsvColor();
HSV to RGB
final HsvColor hsvColor = HsvColor(hueValue, saturationValue, valueValue)
final RgbColor rgbColor = hsvColor.toRgbColor();
HSVColor class is not supported in native dart so we are using a package for that with similar classes.

Related

error diffusion dither image using CIfilter

I am trying to dither an image. I have made some swift code which applies the floyd steinberg dither but it takes a long time to process an image as it isn't wrapped in a cifilter, its just swift code. I am thinking that if I can make a custom cifilter that it would be processed on the gpu and speed up the process. However I am not an expert in CIfilter language.
This is my swift code. I have written the error distribution matrix calculations out in full for the sake of clarity.
internal struct color {
let r: Int
let g: Int
let b: Int
}
func ditherImage2(){
let image = UIImage(named: "image")
let width = Int(image!.size.width)
let height = Int(image!.size.height)
let pixelArray = pixelarray(image)
func offset(row: Int, column: Int) -> Int {
return row * width + column
}
for y in 0 ..< height {
for x in 0 ..< width {
let currentOffset = offset(row: y, column: x)
let currentColor = pixelArray![currentOffset]
// get current colour of pixel
let oldR = currentColor.r
let oldG = currentColor.g
let oldB = currentColor.b
// quantize / reduce the colours to pallet of 6 colours
let factor = 1;
let newR = round(factor * oldR / 255) * (255/factor)
let newG = round(factor * oldG / 255) * (255/factor)
let newB = round(factor * oldB / 255) * (255/factor)
pixelArray[currentOffset] = color(r:newR, g:newG, b:newB)
let errR = oldR - newR;
let errG = oldG - newG;
let errB = oldB - newB;
// distribute the error to the surrounding pixels using floyd stenberg matrix
let index = offset(row:x+1, column:y)
let c = pixelArray[index]
let r = c.r
let g = c.g
let b = c.b
r = r + errR * 7/16.0;
g = g + errG * 7/16.0;
b = b + errB * 7/16.0;
pixelArray[index] = color(r:r, g:g, b:b);
let index2 = offset(row:x-1, column:y+1 );
let c2 = pixelArray[index2]
let r2 = c.r
let g2 = c.g
let b2 = c.b
r2 = r2 + errR * 3/16.0;
g2 = g2 + errG * 3/16.0;
b2 = b2 + errB * 3/16.0;
pixelArray[index] = color(r:r2, g:g2, b:b2);
let index3 = offset(row:x, column:y+1);
let c3 = pixelArray[index3]
let r3 = c.r
let g3 = c.g
let b3 = c.b
r3 = r3 + errR * 5/16.0;
g3 = g3 + errG * 5/16.0;
b3 = b3 + errB * 5/16.0;
pixelArray[index] = color(r:r3, g:g3, b:b3);
let index4 = offset(row:x+1, column:y+1);
let c4 = pixelArray[index]
let r4 = c.r
let g4 = c.g
let b4 = c.b
r4 = r4 + errR * 1/16.0;
g4 = g4 + errG * 1/16.0;
b4 = b4 + errB * 1/16.0;
pixelArray[index] = color(r:r4, g:g4, b:b4);
}
}
}
I Have found this https://github.com/rhoeper/Filterpedia-Swift4 which includes a custom filter for ordered dithering which I could use as a base and attempt to adapt to error diffusion dithering. I would prefer to find an existing custom kernel which does the job before jumping into learning CIfilter language. So I am wondering if anyone has an existing kernel or a link to one?
ordered dithering code
float orderedDither2x2(float colorin, float bx, float by, float errorIntensity)
{
float error = 0.0;
int px = int(bx);
int py = int(by);
if (py == 0) {
if (px == 0) { error = 1.0 / 4.0; }
if (px == 1) { error = 3.0 / 4.0; }
}
if (py == 1) {
if (px == 0) { error = 4.0 / 4.0; }
if (px == 1) { error = 2.0 / 4.0; }
}
return colorin * (error * errorIntensity);
}
kernel vec4 ditherBayer(sampler image, float intensity, float matrix, float palette)
{
vec4 pixel = sample(image, samplerCoord(image));
int msize = int(matrix);
float px = mod(pixel.x, msize >= 5 ? float(4.0) : float(msize));
float py = mod(pixel.y, msize >= 5 ? float(4.0) : float(msize));
float red = pixel.r;
float green = pixel.g;
float blue = pixel.b;
if (msize == 2) {
pixel.r = orderedDither2x2(red, px, py, intensity);
pixel.g = orderedDither2x2(green, px, py, intensity);
pixel.b = orderedDither2x2(blue, px, py, intensity);
}
if (msize == 3) {
pixel.r = orderedDither3x3(red, px, py, intensity);
pixel.g = orderedDither3x3(green, px, py, intensity);
pixel.b = orderedDither3x3(blue, px, py, intensity);
}
if (msize == 4) {
pixel.r = orderedDither4x4(red, px, py, intensity);
pixel.g = orderedDither4x4(green, px, py, intensity);
pixel.b = orderedDither4x4(blue, px, py, intensity);
}
if (msize >= 5) {
pixel.r = orderedDither8x8(red, px, py, intensity);
pixel.g = orderedDither8x8(green, px, py, intensity);
pixel.b = orderedDither8x8(blue, px, py, intensity);
}
if (int(palette) == 0) { return vec4(binary(vec3(pixel.r, pixel.g, pixel.b)), pixel.a); }
if (int(palette) == 1) { return vec4(commodore64(vec3(pixel.r, pixel.g, pixel.b)), pixel.a); }
if (int(palette) == 2) { return vec4(vic20(vec3(pixel.r, pixel.g, pixel.b)), pixel.a); }
if (int(palette) == 3) { return vec4(appleII(vec3(pixel.r, pixel.g, pixel.b)), pixel.a); }
if (int(palette) == 4) { return vec4(zxSpectrumBright(vec3(pixel.r, pixel.g, pixel.b)), pixel.a); }
if (int(palette) == 5) { return vec4(zxSpectrumDim(vec3(pixel.r, pixel.g, pixel.b)), pixel.a); }
return pixel;
}
The problem with Floyd-Steinberg dithering is that it's a serial algorithm – the color value of a result pixel depends on pixels that were previously computed. Core Image (and any kind of SIMD parallelization technique) is not very well suited for these kinds of problems. They are designed to perform the same task on all pixels concurrently.
However, I found some approaches for partially parallelizing the computation of independent pixels on the GPU and even an interesting CPU-GPU-hybrid approach.
Unfortunately, Core Image is probably not the best framework for implementing those techniques since CIFilters are limited in what GPU resources they can leverage (no access to global memory, for example). You could instead use Metal compute shaders directly (instead of through Core Image), which will require a lot more support code, though.
If you don't necessarily need error diffusion, you could still use ordered dithering (which can be highly parallelized) to achieve similar results. I also found a nice article about that. The built-in CIDither filter is probably also using this approach.

How to convert camera image to image?

I want to convert camera image from function startImageStream of camera plugin in Flutter to Image to crop that image but I only find the way to convert to FirebaseVisionImage.
Edit For Color Image
if I understand you clear. You are trying to covnert YUV420 format. The following is code snippet from: https://github.com/flutter/flutter/issues/26348
const shift = (0xFF << 24);
Future<Image> convertYUV420toImageColor(CameraImage image) async {
try {
final int width = image.width;
final int height = image.height;
final int uvRowStride = image.planes[1].bytesPerRow;
final int uvPixelStride = image.planes[1].bytesPerPixel;
print("uvRowStride: " + uvRowStride.toString());
print("uvPixelStride: " + uvPixelStride.toString());
// imgLib -> Image package from https://pub.dartlang.org/packages/image
var img = imglib.Image(width, height); // Create Image buffer
// Fill image buffer with plane[0] from YUV420_888
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
final int uvIndex = uvPixelStride * (x / 2).floor() + uvRowStride * (y / 2).floor();
final int index = y * width + x;
final yp = image.planes[0].bytes[index];
final up = image.planes[1].bytes[uvIndex];
final vp = image.planes[2].bytes[uvIndex];
// Calculate pixel color
int r = (yp + vp * 1436 / 1024 - 179).round().clamp(0, 255);
int g = (yp - up * 46549 / 131072 + 44 - vp * 93604 / 131072 + 91).round().clamp(0, 255);
int b = (yp + up * 1814 / 1024 - 227).round().clamp(0, 255);
// color: 0x FF FF FF FF
// A B G R
img.data[index] = shift | (b << 16) | (g << 8) | r;
}
}
imglib.PngEncoder pngEncoder = new imglib.PngEncoder(level: 0, filter: 0);
List<int> png = pngEncoder.encodeImage(img);
muteYUVProcessing = false;
return Image.memory(png);
} catch (e) {
print(">>>>>>>>>>>> ERROR:" + e.toString());
}
return null;
}
I found, sometimes planes[0] bytes per row is not same as width. In that case, you should do something like the code below.
static image_lib.Image convertYUV420ToImage(CameraImage cameraImage) {
final width = cameraImage.width;
final height = cameraImage.height;
final yRowStride = cameraImage.planes[0].bytesPerRow;
final uvRowStride = cameraImage.planes[1].bytesPerRow;
final uvPixelStride = cameraImage.planes[1].bytesPerPixel!;
final image = image_lib.Image(width, height);
for (var w = 0; w < width; w++) {
for (var h = 0; h < height; h++) {
final uvIndex =
uvPixelStride * (w / 2).floor() + uvRowStride * (h / 2).floor();
final index = h * width + w;
final yIndex = h * yRowStride + w;
final y = cameraImage.planes[0].bytes[yIndex];
final u = cameraImage.planes[1].bytes[uvIndex];
final v = cameraImage.planes[2].bytes[uvIndex];
image.data[index] = yuv2rgb(y, u, v);
}
}
return image;
}
static int yuv2rgb(int y, int u, int v) {
// Convert yuv pixel to rgb
var r = (y + v * 1436 / 1024 - 179).round();
var g = (y - u * 46549 / 131072 + 44 - v * 93604 / 131072 + 91).round();
var b = (y + u * 1814 / 1024 - 227).round();
// Clipping RGB values to be inside boundaries [ 0 , 255 ]
r = r.clamp(0, 255);
g = g.clamp(0, 255);
b = b.clamp(0, 255);
return 0xff000000 |
((b << 16) & 0xff0000) |
((g << 8) & 0xff00) |
(r & 0xff);
}

Direct read data from a file png

is required to read data from a binary file without loading them in Bitmap because it is too much, more than 20000x20000 pixels, I need to open a file, one line at a time to read a file for processing. found an example for reading BMP, can not understand how in the same way to get data from PNG.
byte[] B = File.ReadAllBytes(filename);
GCHandle GCH = GCHandle.Alloc(B, GCHandleType.Pinned);
IntPtr Scan0 = (IntPtr)((int)(GCH.AddrOfPinnedObject()) + 54);
int W = Marshal.ReadInt32(Scan0, -36);
int H = Marshal.ReadInt32(Scan0, -32);
Bitmap Bmp = new Bitmap(W, H, 4 * W, PixelFormat.Format32bppArgb, Scan0);
GCH.Free();
return Bmp;
Language C#
I found a library (PNGChunkParser) with parsing blocks APG has received all the blocks that are in the file, I found a 21 unit Idate, but trying to paint them 50 lines, to be not the right picture:
byte[] B = File.ReadAllBytes(filename);
List<byte> tmpb = new List<byte>();
using (MemoryStream stream = new MemoryStream(B))
{
PNGChunk[] chunks = PNGChunkParser.ChunksFromStream(stream).ToArray();
foreach (PNGChunk item in chunks)
{
if (item.TypeString == "IDAT")
{
PNGChunk idatChunk = item;
foreach (byte s in idatChunk.Data)
{
tmpb.Add(s);
}
}
}
}
var size_png = ImageHelper.GetDimensions(filename);
GetConturPic(tmpb.ToArray(), size_png.Width, 50,PixelFormat.Format32bppArgb);
private void GetConturPic(byte[] data, int w, int h, PixelFormat pixel_format)
{
int index;
int stride = GetStride(w, pixel_format);
Bitmap bm = new Bitmap(w, h);
Color fm = new Color();
try
{
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
index = y * stride + 4 * x;
fm = GetPixelColor(data, w, h, x, y,
System.Drawing.Bitmap.GetPixelFormatSize(pixel_format));
bm.SetPixel(x, y, fm);
}
pictureBox1.Image = bm;
pictureBox1.Refresh();
}
}
catch (Exception ex)
{
listBox1.Items.Add(ex.Message);
}
}
public Color GetPixelColor(byte[] Pixels, int w, int h, int x, int y, int depth)
{
Color clr = Color.Empty;
// Get color components count
int cCount = depth / 8;
// Get start index of the specified pixel
int i = ((y * w) + x) * cCount;
if (i > Pixels.Length - cCount)
throw new IndexOutOfRangeException();
byte b, g, a, r, c;
switch (depth)
{
case 32:
b = Pixels[i];
g = Pixels[i + 1];
r = Pixels[i + 2];
a = Pixels[i + 3]; // a
clr = Color.FromArgb(a, b, g, r);
break;
case 24:
b = Pixels[i];
g = Pixels[i + 1];
r = Pixels[i + 2];
clr = Color.FromArgb(b, g, r);
break;
case 8:
c = Pixels[i];
clr = Color.FromArgb(c, c, c);
break;
}
return clr;
}
help what need to do to get 50 lines of image ?

How to make the blackboard text appear clearer using MATLAB?

What are the sequence of filters I should put if I want the final image to be more clearer with a digital type look. I mean only two distinct colors, one for the board and one for the chalk writing.
When it comes to identifying text in images you better use Stroke Width Transform.
Here's a little result I obtained on your image (the basic transform + connected component w/o filtering):
My mex implementation based on code from here
#include "mex.h"
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <math.h>
using namespace std;
#define PI 3.14159265
struct Point2d {
int x;
int y;
float SWT;
};
struct Point2dFloat {
float x;
float y;
};
struct Ray {
Point2d p;
Point2d q;
std::vector<Point2d> points;
};
void strokeWidthTransform(const float * edgeImage,
const float * gradientX,
const float * gradientY,
bool dark_on_light,
float * SWTImage,
int h, int w,
std::vector<Ray> & rays) {
// First pass
float prec = .05f;
for( int row = 0; row < h; row++ ){
const float* ptr = edgeImage + row*w;
for ( int col = 0; col < w; col++ ){
if (*ptr > 0) {
Ray r;
Point2d p;
p.x = col;
p.y = row;
r.p = p;
std::vector<Point2d> points;
points.push_back(p);
float curX = (float)col + 0.5f;
float curY = (float)row + 0.5f;
int curPixX = col;
int curPixY = row;
float G_x = gradientX[ col + row*w ];
float G_y = gradientY[ col + row*w ];
// normalize gradient
float mag = sqrt( (G_x * G_x) + (G_y * G_y) );
if (dark_on_light){
G_x = -G_x/mag;
G_y = -G_y/mag;
} else {
G_x = G_x/mag;
G_y = G_y/mag;
}
while (true) {
curX += G_x*prec;
curY += G_y*prec;
if ((int)(floor(curX)) != curPixX || (int)(floor(curY)) != curPixY) {
curPixX = (int)(floor(curX));
curPixY = (int)(floor(curY));
// check if pixel is outside boundary of image
if (curPixX < 0 || (curPixX >= w) || curPixY < 0 || (curPixY >= h)) {
break;
}
Point2d pnew;
pnew.x = curPixX;
pnew.y = curPixY;
points.push_back(pnew);
if ( edgeImage[ curPixY*w+ curPixX ] > 0) {
r.q = pnew;
// dot product
float G_xt = gradientX[ curPixY*w + curPixX ];
float G_yt = gradientY[ curPixY*w + curPixX ];
mag = sqrt( (G_xt * G_xt) + (G_yt * G_yt) );
if (dark_on_light){
G_xt = -G_xt/mag;
G_yt = -G_yt/mag;
} else {
G_xt = G_xt/mag;
G_yt = G_yt/mag;
}
if (acos(G_x * -G_xt + G_y * -G_yt) < PI/2.0 ) {
float length = sqrt( ((float)r.q.x - (float)r.p.x)*((float)r.q.x - (float)r.p.x) + ((float)r.q.y - (float)r.p.y)*((float)r.q.y - (float)r.p.y));
for (std::vector<Point2d>::iterator pit = points.begin(); pit != points.end(); pit++) {
float* pSWT = SWTImage + w * pit->y + pit->x;
if (*pSWT < 0) {
*pSWT = length;
} else {
*pSWT = std::min(length, *pSWT);
}
}
r.points = points;
rays.push_back(r);
}
break;
}
}
}
}
ptr++;
}
}
}
bool Point2dSort(const Point2d &lhs, const Point2d &rhs) {
return lhs.SWT < rhs.SWT;
}
void SWTMedianFilter(float * SWTImage, int h, int w,
std::vector<Ray> & rays, float maxWidth = -1 ) {
for (std::vector<Ray>::iterator rit = rays.begin(); rit != rays.end(); rit++) {
for (std::vector<Point2d>::iterator pit = rit->points.begin(); pit != rit->points.end(); pit++) {
pit->SWT = SWTImage[ w*pit->y + pit->x ];
}
std::sort(rit->points.begin(), rit->points.end(), &Point2dSort);
//std::nth_element( rit->points.begin(), rit->points.end(), rit->points.size()/2, &Point2dSort );
float median = (rit->points[rit->points.size()/2]).SWT;
if ( maxWidth > 0 && median >= maxWidth ) {
median = -1;
}
for (std::vector<Point2d>::iterator pit = rit->points.begin(); pit != rit->points.end(); pit++) {
SWTImage[ w*pit->y + pit->x ] = std::min(pit->SWT, median);
}
}
}
typedef std::vector< std::set<int> > graph_t; // graph as a list of neighbors per node
void connComp( const graph_t& g, std::vector<int>& c, int i, int l ) {
// starting from node i labe this conn-comp with label l
if ( i < 0 || i > g.size() ) {
return;
}
std::vector< int > stack;
// push i
stack.push_back(i);
c[i] = l;
while ( ! stack.empty() ) {
// pop
i = stack.back();
stack.pop_back();
// go over all nieghbors
for ( std::set<int>::const_iterator it = g[i].begin(); it != g[i].end(); it++ ) {
if ( c[*it] < 0 ) {
stack.push_back( *it );
c[ *it ] = l;
}
}
}
}
int findNextToLabel( const graph_t& g, const vector<int>& c ) {
for ( int i = 0 ; i < c.size(); i++ ) {
if ( c[i] < 0 ) {
return i;
}
}
return c.size();
}
int connected_components(const graph_t& g, vector<int>& c) {
// check for empty graph!
if ( g.empty() ) {
return 0;
}
int i = 0;
int num_conn = 0;
do {
connComp( g, c, i, num_conn );
num_conn++;
i = findNextToLabel( g, c );
} while ( i < g.size() );
return num_conn;
}
std::vector< std::vector<Point2d> >
findLegallyConnectedComponents(const float* SWTImage, int h, int w,
std::vector<Ray> & rays) {
std::map<int, int> Map;
std::map<int, Point2d> revmap;
std::vector<std::vector<Point2d> > components; // empty
int num_vertices = 0, idx = 0;
graph_t g;
// Number vertices for graph. Associate each point with number
for( int row = 0; row < h; row++ ){
for (int col = 0; col < w; col++ ){
idx = col + w * row;
if (SWTImage[idx] > 0) {
Map[idx] = num_vertices;
Point2d p;
p.x = col;
p.y = row;
revmap[num_vertices] = p;
num_vertices++;
std::set<int> empty;
g.push_back(empty);
}
}
}
if ( g.empty() ) {
return components; // nothing to do with an empty graph...
}
for( int row = 0; row < h; row++ ){
for (int col = 0; col < w; col++ ){
idx = col + w * row;
if ( SWTImage[idx] > 0) {
// check pixel to the right, right-down, down, left-down
int this_pixel = Map[idx];
float thisVal = SWTImage[idx];
if (col+1 < w) {
float right = SWTImage[ w*row + col + 1 ];
if (right > 0 && (thisVal/right <= 3.0 || right/thisVal <= 3.0)) {
g[this_pixel].insert( Map[ w*row + col + 1 ] );
g[ Map[ w*row + col + 1 ] ].insert( this_pixel );
//boost::add_edge(this_pixel, map.at(row * SWTImage->width + col + 1), g);
}
}
if (row+1 < h) {
if (col+1 < w) {
float right_down = SWTImage[ w*(row+1) + col + 1 ];
if (right_down > 0 && (thisVal/right_down <= 3.0 || right_down/thisVal <= 3.0)) {
g[ this_pixel ].insert( Map[ w*(row+1) + col + 1 ] );
g[ Map[ w*(row+1) + col + 1 ] ].insert(this_pixel);
// boost::add_edge(this_pixel, map.at((row+1) * SWTImage->width + col + 1), g);
}
}
float down = SWTImage[ w*(row+1) + col ];
if (down > 0 && (thisVal/down <= 3.0 || down/thisVal <= 3.0)) {
g[ this_pixel ].insert( Map[ w*(row+1) + col ] );
g[ Map[ w*(row+1) + col ] ].insert( this_pixel );
//boost::add_edge(this_pixel, map.at((row+1) * SWTImage->width + col), g);
}
if (col-1 >= 0) {
float left_down = SWTImage[ w*(row+1) + col - 1 ];
if (left_down > 0 && (thisVal/left_down <= 3.0 || left_down/thisVal <= 3.0)) {
g[ this_pixel ].insert( Map[ w*(row+1) + col - 1 ] );
g[ Map[ w*(row+1) + col - 1 ] ].insert( this_pixel );
//boost::add_edge(this_pixel, map.at((row+1) * SWTImage->width + col - 1), g);
}
}
}
}
}
}
std::vector<int> c(num_vertices, -1);
int num_comp = connected_components(g, c);
components.reserve(num_comp);
//std::cout << "Before filtering, " << num_comp << " components and " << num_vertices << " vertices" << std::endl;
for (int j = 0; j < num_comp; j++) {
std::vector<Point2d> tmp;
components.push_back( tmp );
}
for (int j = 0; j < num_vertices; j++) {
Point2d p = revmap[j];
(components[c[j]]).push_back(p);
}
return components;
}
enum {
EIN = 0,
GXIN,
GYIN,
DOLFIN,
MAXWIN,
NIN };
void mexFunction( int nout, mxArray* pout[], int nin, const mxArray* pin[] ) {
//
// make sure images are input in transposed so that they are arranged row-major in memory
//
mxAssert( nin == NIN, "wrong number of inputs" );
mxAssert( nout > 1, "only one output" );
int h = mxGetN( pin[EIN] ); // inputs are transposed!
int w = mxGetM( pin[EIN] );
mxAssert( mxIsClass( pin[EIN], mxSINGLE_CLASS ) && h == mxGetN( pin[EIN] ) && w == mxGetM( pin[EIN] ), "edge map incorrect");
mxAssert( mxIsClass( pin[GXIN], mxSINGLE_CLASS ) && h == mxGetN( pin[GXIN] ) && w == mxGetM( pin[GXIN] ), "edge map incorrect");
mxAssert( mxIsClass( pin[GYIN], mxSINGLE_CLASS ) && h == mxGetN( pin[GYIN] ) && w == mxGetM( pin[GYIN] ), "edge map incorrect");
const float * edgeImage = (float*) mxGetData( pin[EIN] );
const float * gradientX = (float*) mxGetData( pin[GXIN] );
const float * gradientY = (float*) mxGetData( pin[GYIN] );
bool dark_on_light = mxGetScalar( pin[DOLFIN] ) != 0 ;
float maxWidth = mxGetScalar( pin[MAXWIN] );
// allocate output
pout[0] = mxCreateNumericMatrix( w, h, mxSINGLE_CLASS, mxREAL );
float * SWTImage = (float*) mxGetData( pout[0] );
// set SWT to -1
for ( int i = 0 ; i < w*h; i++ ) {
SWTImage[i] = -1;
}
std::vector<Ray> rays;
strokeWidthTransform ( edgeImage, gradientX, gradientY, dark_on_light, SWTImage, h, w, rays );
SWTMedianFilter ( SWTImage, h, w, rays, maxWidth );
// connected components
if ( nout > 1 ) {
// Calculate legally connect components from SWT and gradient image.
// return type is a vector of vectors, where each outer vector is a component and
// the inner vector contains the (y,x) of each pixel in that component.
std::vector<std::vector<Point2d> > components = findLegallyConnectedComponents(SWTImage, h, w, rays);
pout[1] = mxCreateNumericMatrix( w, h, mxSINGLE_CLASS, mxREAL );
float* pComp = (float*) mxGetData( pout[1] );
for ( int i = 0 ; i < w*h; i++ ) {
pComp[i] = 0;
}
for ( int ci = 0 ; ci < components.size(); ci++ ) {
for ( std::vector<Point2d>::iterator it = components[ci].begin() ; it != components[ci].end(); it++ ) {
pComp[ w * it->y + it->x ] = ci + 1;
}
}
}
}
Matlab function calling stroke-width-transform (SWT) mex-file:
function [swt swtcc] = SWT( img, dol, maxWidth )
if size( img, 3 ) == 3
img = rgb2gray(img);
end
img = im2single(img);
edgeMap = single( edge( img, 'canny', .15 ) );
img = imfilter( img, fspecial('gauss',[5 5], 0.3*(2.5-1)+.8) );
gx = imfilter( img, fspecial('prewitt')' ); %//'
gy = imfilter( img, fspecial('prewitt') );
gx = single(medfilt2( gx, [3 3] ));
gy = single(medfilt2( gy, [3 3] ));
[swt swtcc] = swt_mex( edgeMap.', gx.', gy.', dol, maxWidth ); %//'
swt = swt'; %//'
swtcc = double(swtcc'); %//'
Try this :
I = imread('...'); % Your board image
ThreshConstant = 1; % Try to vary this constant.
bw = im2bw(I , ThreshConstant * graythresh(I)); % Black-white image
SegmentedImg = I.*repmat(uint8(bw), [1 1 3]);
Just do imshow(bw); and you will have a 2 color image normally well segmented.
If the threshold is too strong, try to turn around 0.5 to 1.5 with ThreshConstant.
or you could try this
im = imread('http://i.imgur.com/uJIXp13.jpg'); %the image posted above
im2=rgb2gray(im);
maxp=uint16(max(max(im2)));
minp=uint16(min(min(im2)));
bw=im2bw(im2,(double(minp+maxp))/(2*255)); %the threshold as alexandre said, but with the min max idensity as threshold
bw=~bw; % you need to reverse from black font - whit letters to black letters white font :P
imshow(bw)
this should be the result
have in mind , that you can use this technique adaptively with a window, finding the threshold of the window every time for best results.

How to retrieve HSB from CGColor?

I have a CGColor in my app and I'd like to get the hue, saturation and brightness values from it. How would i be able to access these?
See Duncan Champney's post to cocoa-dev entitled "Here is code to Convert RGB <-> HSB". There, he gives this code:
//------------------
//---header file
#define RETURN_HSV(h, s, v) {HSV.H = h; HSV.S = s; HSV.V = v; return
HSV;}
#define RETURN_RGB(r, g, b) {RGB.R = r; RGB.G = g; RGB.B = b; return
RGB;}
#define UNDEFINED 0
// Theoretically, hue 0 (pure red) is identical to hue 6 in these
transforms. Pure
// red always maps to 6 in this implementation. Therefore UNDEFINED
can be
// defined as 0 in situations where only unsigned numbers are desired.
typedef struct {float R, G, B;} RGBType;
typedef struct {float H, S, V;} HSVType;
//------------------
//--The code
#include <math.h>
HSVType RGB_to_HSV( RGBType RGB )
{
// RGB are each on [0, 1]. S and V are returned on [0, 1] and H is
// returned on [0, 1]. Exception: H is returned UNDEFINED if S==0.
float R = RGB.R, G = RGB.G, B = RGB.B, v, x, f;
int i;
HSVType HSV;
//x = fminx(R, G, B);
x = fminf(R, G);
x = fminf(x, B);
//v = fmaxf(R, G, B);
v = fmaxf(R, G);
v = fmaxf(v, B);
if(v == x) RETURN_HSV(UNDEFINED, 0, v);
f = (R == x) ? G - B : ((G == x) ? B - R : R - G);
i = (R == x) ? 3 : ((G == x) ? 5 : 1);
RETURN_HSV(((i - f /(v - x))/6), (v - x)/v, v);
}
RGBType HSV_to_RGB( HSVType HSV )
{
// H is given on [0, 1] or UNDEFINED. S and V are given on [0, 1].
// RGB are each returned on [0, 1].
float h = HSV.H * 6, s = HSV.S, v = HSV.V, m, n, f;
int i;
RGBType RGB;
if (h == 0) h=.01;
if(h == UNDEFINED) RETURN_RGB(v, v, v);
i = floorf(h);
f = h - i;
if(!(i & 1)) f = 1 - f; // if i is even
m = v * (1 - s);
n = v * (1 - s * f);
switch (i)
{
case 6:
case 0: RETURN_RGB(v, n, m);
case 1: RETURN_RGB(n, v, m);
case 2: RETURN_RGB(m, v, n);
case 3: RETURN_RGB(m, n, v);
case 4: RETURN_RGB(n, m, v);
case 5: RETURN_RGB(v, m, n);
}
RETURN_RGB(0, 0, 0);
}
//------------------