Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Gráficos
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 30-08-2007
kapullok_2006 kapullok_2006 is offline
Miembro
 
Registrado: mar 2007
Posts: 90
Poder: 18
kapullok_2006 Va por buen camino
Unhappy convertir imagen blanco-negro a color

hola a todos, no encuentro informacion sobre esta conversion.Busco un componente o funcion para ello.Sé que hay q usar la funcion RGB().Pero...
es que no la entiendo.Me dais alguna pista?

Saludos.
Responder Con Cita
  #2  
Antiguo 30-08-2007
Avatar de Delphius
[Delphius] Delphius is offline
Miembro Premium
 
Registrado: jul 2004
Ubicación: Salta, Argentina
Posts: 5.582
Poder: 25
Delphius Va camino a la fama
Hola kapullok_2006, Dejame ver si entendí:
¿Deseas convertir una imagen de blanco y negro a color? Dejame decirte que no se puede. Una imagen en blanco y negro no contiene demasiada "información" para predecir que color le debe ser correspondido a una zona.
Al tratarse de una imagen "binaria" no hay manera de decidir que color asignar. Es 255 o 0 (blanco y negro respectivamente) el color que puede asumir un pixel. No hay manera de decir en una "mancha" que color ha de asignarsele.

Ahora, si por "blanco y negro" debo entender que se trata de niveles de gris. Es otra situación. Si bien no puede saberse que color responder, puede predecirse o estimarse. Existe una técnica llamada Pseudo-color que lo que hace es asignar a un nivel de gris un nivel de otro color determinado. Un ejemplo de este tipo son las radiografías, que emplea diversos niveles de azul para resaltar distintas zonas.

Te aconsejaría que busques en Google sobre pseudo-color.

Con respecto a las funciones que puedes usar todo dependerá de como prosigas. Hay dos maneras de acceder a un pixel, por un lado tienes la propiedad Pixels[] y por otro lado (que es más óptimo) mediante Scanline.
Te puede ser de utilidad este truco
Y también algo de esto

Por si tienes dudas, consulta en estos foros bajo Scanline y Pixels.
Saludos,
__________________
Delphius
[Guia de estilo][Buscar]
Responder Con Cita
  #3  
Antiguo 30-08-2007
Avatar de paldave
paldave paldave is offline
Miembro
 
Registrado: ago 2007
Ubicación: Uruguay
Posts: 148
Poder: 17
paldave Va por buen camino
Fijate en estas rutinas relacionadas al manejo de color de una imagen llamadas ConvertToGrayScale, ConvertToMonocrome, Iluminate y Negativize:
Código Delphi [-]
procedure TosImage.ConvertToGrayScale(Light:Integer=0;Tint:TColor=clBlack);
var f,n:Integer;
c:TColor;
bm:TBitmap;
begin
  if not (Picture.Graphic is TBitmap) then
  begin
    raise EInvalidGraphicOperation.Create('Image must be a bitmap.');
    exit;
  end;

  if Picture.Bitmap =nil then
  begin
    raise EInvalidGraphic.Create('Bitmap do not exists.');
    exit;
  end;

  bm:=TBitmap.Create;
  bm.Width :=Picture.Bitmap.Width;
  bm.Height :=Picture.Bitmap.Height;
  for f:=0 to Picture.Bitmap.Height do
    for n:=0 to Picture.Bitmap.Width do
    begin
      c:=ColorToGrayScale(Canvas.Pixels[n,f]);
      if Light <> 0 then
        c:= ChangeColorLight(c,Light);

      if Tint <> clBlack then
        c:=GrayScaleToColorScale(ColorToGrayValue(c),Tint);

      bm.Canvas.Pixels[n,f]:=c;
    end;
  Picture.Bitmap.Assign(bm);
  bm.free;
end;
Código Delphi [-]
procedure TosImage.ConvertToMonochrome(Range:Byte;ForeColor:TColor=clBlack;BackColor:TColor=clWhite);
var f,n:Integer;
t:Byte;
bm:TBitmap;
begin
  if not (Picture.Graphic is TBitmap) then
  begin
    raise EInvalidGraphicOperation.Create('Image must be a bitmap.');
    exit;
  end;

  if Picture.Bitmap =nil then
  begin
    raise EInvalidGraphic.Create('Bitmap do not exists.');
    exit;
  end;

  bm:=TBitmap.Create;
  bm.Width :=Picture.Bitmap.Width;
  bm.Height:=Picture.Bitmap.Height;
  for f:=0 to Picture.Bitmap.Height do
    for n:=0 to Picture.Bitmap.Width do
    begin
      t:=ColorToGrayValue(Canvas.Pixels[n,f]);
      if t <= Range then
         bm.Canvas.Pixels[n,f]:=ForeColor
       else
         bm.Canvas.Pixels[n,f]:=BackColor;
    end;
  Picture.Bitmap.Assign(bm);
  bm.free;
end;
Código Delphi [-]
procedure TosImage.Iluminate(Light:Integer);
var f,n:Integer;
c:TColor;
bm:TBitmap;
begin
  if not (Picture.Graphic is TBitmap) then
  begin
    raise EInvalidGraphicOperation.Create('Image must be a bitmap.');
    exit;
  end;

  if Picture.Bitmap =nil then
  begin
    raise EInvalidGraphic.Create('Bitmap do not exists.');
    exit;
  end;

  bm:=TBitmap.Create;
  bm.Width :=Picture.Bitmap.Width;
  bm.Height :=Picture.Bitmap.Height;
  for f:=0 to Picture.Bitmap.Height do
    for n:=0 to Picture.Bitmap.Width do
    begin
      c:=Canvas.Pixels[n,f];
      if Light <> 0 then
        c:= ChangeColorLight(c,Light);

      bm.Canvas.Pixels[n,f]:=c;
    end;
  Picture.Bitmap.Assign(bm);
  bm.free;
end;
Código Delphi [-]
procedure TosImage.Negativize;
var f,n:Integer;
r,g,b:Byte;
bm:TBitmap;
begin
  if not (Picture.Graphic is TBitmap) then
  begin
    raise EInvalidGraphicOperation.Create('Image must be a bitmap.');
    exit;
  end;

  if Picture.Bitmap =nil then
  begin
    raise EInvalidGraphic.Create('Bitmap do not exists.');
    exit;
  end;

  bm:=TBitmap.Create;
  bm.Width :=Picture.Bitmap.Width;
  bm.Height :=Picture.Bitmap.Height;
  for f:=0 to Picture.Bitmap.Height do
    for n:=0 to Picture.Bitmap.Width do
    begin
      DecodeColor(Canvas.Pixels[n,f],r,g,b);
      r:=255-r;
      g:=255-g;
      b:=255-b;
      bm.Canvas.Pixels[n,f]:=EncodeColor(r,g,b);
    end;
  Picture.Bitmap.Assign(bm);
  bm.free;
end;
Código Delphi [-]
procedure TosImage.Negativize;
var f,n:Integer;
r,g,b:Byte;
bm:TBitmap;
begin
  if not (Picture.Graphic is TBitmap) then
  begin
    raise EInvalidGraphicOperation.Create('Image must be a bitmap.');
    exit;
  end;

  if Picture.Bitmap =nil then
  begin
    raise EInvalidGraphic.Create('Bitmap do not exists.');
    exit;
  end;

  bm:=TBitmap.Create;
  bm.Width :=Picture.Bitmap.Width;
  bm.Height :=Picture.Bitmap.Height;
  for f:=0 to Picture.Bitmap.Height do
    for n:=0 to Picture.Bitmap.Width do
    begin
      DecodeColor(Canvas.Pixels[n,f],r,g,b);
      r:=255-r;
      g:=255-g;
      b:=255-b;
      bm.Canvas.Pixels[n,f]:=EncodeColor(r,g,b);
    end;
  Picture.Bitmap.Assign(bm);
  bm.free;
end;
Puedes agregarlas en un control que sea descendiente directo de TImage;
También necesitarás agregar en ese control estas otras rutinas que son llamadas por las anteriores:
Código Delphi [-]
procedure DecodeCMYK(CMYKColor:Integer; var C,M,Y,K:Byte);
begin
  C:= GetCValue(CMYKColor);
  M:= GetMValue(CMYKColor);
  Y:= GetYValue(CMYKColor);
  K:= GetKValue(CMYKColor);
end;
Código Delphi [-]
function EncodeColor(R,G,B:Byte):TColor;
begin
   Result:= RGB(R,G,B);
end;
Código Delphi [-]
procedure DecodeColor(Color:TColor; var R,G,B:Byte);
begin
  Color:=ColorToRGB(Color);
  R:= GetRValue(Color);
  G:= GetGValue(Color);
  B:= GetBValue(Color);
end;
Código Delphi [-]
procedure DecodeRGB(RGBColor:Integer; var R,G,B:Byte);
begin
  R:= GetRValue(RGBColor);
  G:= GetGValue(RGBColor);
  B:= GetBValue(RGBColor);
end;
Código Delphi [-]
function ColorToGrayScale(Color:TColor):TColor;
var R,G,B:Byte;
begin
  DecodeColor(Color,R,G,B);
  R:=R*76 div 255;
  G:=G*152 div 255;
  B:=B*24 div 255;
  R:=R+G+B;
  Result:=EncodeColor(R,R,R);
end;
Código Delphi [-]
function ColorToGrayValue(Color:TColor):Byte;
var r,g,b:Byte;
begin
  DecodeColor(Color,R,G,B);
  R:=R*76 div 255;
  G:=G*152 div 255;
  B:=B*24 div 255;
  Result:=R+G+B;
end;
Código Delphi [-]
function BrightenColor(Color:TColor; Change:Byte):TColor;
var r,g,b:Integer;
begin
  result:=ColorToRGB(color);
  r:= GetRValue(result);
  g:= GetGValue(result);
  b:= GetBValue(result);

  inc(r,Change);
  inc(g,Change);
  inc(b,Change);

  if r>255 then r:=255;
  if g>255 then g:=255;
  if b>255 then b:=255;

  result:= RGB(r,g,b);
end;
Código Delphi [-]
function DarkenColor(Color:TColor; Change:Byte):TColor;
var r,g,b:Integer;
begin
  result:=ColorToRGB(color);
  r:= GetRValue(result);
  g:= GetGValue(result);
  b:= GetBValue(result);

  dec(r,Change);
  dec(g,Change);
  dec(b,Change);

  if r < 0 then r:=0;
  if g < 0 then g:=0;
  if b < 0 then b:=0;

  result:= RGB(r,g,b);
end;
Código Delphi [-]
function ChangeColorLight(Color:TColor;Change:Integer):TColor;
begin
  if Change>0 then
    result:=BrightenColor(Color,Change)
  else if Change < 0 then
    result:=DarkenColor(Color,abs(Change));
end;
Código Delphi [-]
function GrayScaleToColorScale(GrayValue:Byte;Color:TColor):TColor;
var r,g,b:Byte;
begin
  DecodeColor(Color,r,g,b);
  r:= r+((GrayValue*(256-r))div 256);
  g:= g+((GrayValue*(256-g))div 256);
  b:= b+((GrayValue*(256-b))div 256);
  result:=EncodeColor(r,g,b);
end;
Código Delphi [-]
function ApproachColor(FromColor,ToColor:TColor;Approach:Byte):TColor;
var r1,r2,g1,g2,b1,b2:Byte;
begin
  DecodeColor(FromColor,r1,g1,b1);
  DecodeColor(ToColor,r2,g2,b2);

  if r1 < r2 then
  begin
    if r1+Approach < r2 then
      r1:=r1+Approach
    else
      r1:=r2;
  end
  else
  begin
    if r1-Approach>r2 then
      r1:=r1-Approach
    else
      r1:=r2;
  end;

  if g1 < g2 then
  begin
    if g1+Approach < g2 then
      g1:=g1+Approach
    else
      g1:=g2;
  end
  else
  begin
    if g1-Approach>g2 then
      g1:=g1-Approach
    else
      g1:=g2;
  end;


  if b1 < b2 then
  begin
    if b1+Approach < b2 then
      b1:=b1+Approach
    else
      b1:=b2;
  end
  else
  begin
    if b1-Approach>b2 then
      b1:=b1-Approach
    else
      b1:=b2;
  end;

  result:=EncodeColor(r1,g1,b1);
end;
No están optimizadas para velocidad (no me tomé ese trabajo al escribirlas) pero no son muy lentas

Última edición por paldave fecha: 30-08-2007 a las 23:02:34. Razón: El formateador del foro cambiaba el código!!!!!
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Blanco y Negro chona API de Windows 0 31-08-2006 16:58:38
Imagen blanco y negro yorllito Gráficos 4 04-04-2006 23:13:50
Un negro y Un blanco Ambar Humor 4 24-03-2004 19:13:57
Pasar JPG color a blanco y negro pampitasnowman Gráficos 2 01-10-2003 17:55:43
Blanco & Negro craven Gráficos 1 16-05-2003 22:01:43


La franja horaria es GMT +2. Ahora son las 00:02:45.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi