Jython 图像处理

发布于 2024-08-22 13:31:54 字数 1534 浏览 3 评论 0原文

该程序应该提取图像的轮廓,然后将其分成不同的象限,然后为其着色,例如安迪·沃霍尔玛丽莲·梦露的照片。

“Warholize”函数之前的每个函数都可以工作,但它卡在 warholize 函数下的 c=getPixel(picEdge,x,y) 上,我不确定该怎么办。任何帮助将不胜感激。它应该做“让 c 为 picEdge 中位置 x,y 处像素的颜色”

def main():
  pic= makePicture( pickAFile() )
  show( pic )
  threshold= 10
  edgePic= makeOutline( pic, threshold )
  warholize(pic)
  show(warholize(pic))

def difference( a, b ):
  if a > b :
    return a - b
  else:
    return b - a

def intensity( px ) :
  r= getRed( px )
  g= getBlue( px )
  b= getGreen( px )
  avg= ( r + g + b ) / 3
  return avg

def makeOutline( pic, threshold ):
  w= getWidth( pic )
  h= getHeight( pic )
  edgePic= makeEmptyPicture( w, h )
  for x in range(2,w-1) :
    for y in range(2,h-1):
      px= getPixel( pic, x, y )
      pxLeft= getPixel( pic, x-1, y )
      pxUp= getPixel( pic, x, y-1 )
      leftDiff= difference( intensity(pxLeft), intensity(px) )
      upDiff= difference( intensity(pxUp), intensity(px) )
      if leftDiff > threshold or upDiff > threshold :
        setColor( getPixel(edgePic,x,y), black )   

def warholize(pic):
    threshold=10
    picEdge=makeOutline(pic,threshold)
    w= getWidth( pic )
    h= getHeight( pic )
    picNew= makeEmptyPicture( w, h )

    for x in range(0,w,2):
        for y in range (0,h,2):
           c=getPixel(picEdge,x,y)
           px=getPixel(picNew,x/2,y/2)
           if c is black:
               setColor(px,blue)
           else:
               setColor(px,yellow)
    return picNew

This program is supposed to take the outline of an image, then split it into different quadrants, then color it, such as the Andy Warhol Marilyn Monroe picture.

Every function up to the "Warholize" function works but it gets stuck on c=getPixel(picEdge,x,y) under the warholize function at which I'm not sure what to do. Any help would be greatly appreciated. It is supposed to do "let c be the color of the pixel in picEdge at location x,y "

def main():
  pic= makePicture( pickAFile() )
  show( pic )
  threshold= 10
  edgePic= makeOutline( pic, threshold )
  warholize(pic)
  show(warholize(pic))

def difference( a, b ):
  if a > b :
    return a - b
  else:
    return b - a

def intensity( px ) :
  r= getRed( px )
  g= getBlue( px )
  b= getGreen( px )
  avg= ( r + g + b ) / 3
  return avg

def makeOutline( pic, threshold ):
  w= getWidth( pic )
  h= getHeight( pic )
  edgePic= makeEmptyPicture( w, h )
  for x in range(2,w-1) :
    for y in range(2,h-1):
      px= getPixel( pic, x, y )
      pxLeft= getPixel( pic, x-1, y )
      pxUp= getPixel( pic, x, y-1 )
      leftDiff= difference( intensity(pxLeft), intensity(px) )
      upDiff= difference( intensity(pxUp), intensity(px) )
      if leftDiff > threshold or upDiff > threshold :
        setColor( getPixel(edgePic,x,y), black )   

def warholize(pic):
    threshold=10
    picEdge=makeOutline(pic,threshold)
    w= getWidth( pic )
    h= getHeight( pic )
    picNew= makeEmptyPicture( w, h )

    for x in range(0,w,2):
        for y in range (0,h,2):
           c=getPixel(picEdge,x,y)
           px=getPixel(picNew,x/2,y/2)
           if c is black:
               setColor(px,blue)
           else:
               setColor(px,yellow)
    return picNew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈年往事 2024-08-29 13:31:54

我将差异函数和强度函数放入 makeOutline 函数中。我已经从 makeOutline 函数调用了 warholize 函数。
另外,您需要获取单独象限的颜色,在这里,我刚刚使用 getRed Pixel 来查看它是黑色还是白色(全彩与否)。

在本例中,我使用阈值 100 来优化效果。我已经在 makeOutline 函数中设置了阈值,您可以根据需要使用它。

  def main():
  pic= makePicture( pickAFile() )
  show(pic)
  newPic= makeOutline(pic )
  show(newPic)

图片

def makeOutline(pic ):
  picEdge=makeEmptyPicture(getWidth(pic),getHeight(pic))
  for x in range (0, getWidth(pic)-1):
    for y in range (0, getHeight(pic)-1):
      here=getPixel(picEdge,x,y)
      down = getPixel(pic,x,y+1)
      right = getPixel(pic, x+1,y)
      hereL=(getRed(here)+getGreen(here)+getBlue(here))/3
      downL=(getRed(down)+getGreen(down)+getBlue(down))/3
      rightL=(getRed(right)+getGreen(right)+getBlue(right))/3
      if abs (hereL-downL)>100 and abs(hereL-rightL)>100:
        setColor(here,black)
      if abs (hereL-downL)<=100 or abs(hereL-rightL)<=100:
        setColor(here,white)
  warholizedPic=warholize(picEdge)
  return warholizedPic

def warholize(picEdge):
  w= getWidth( picEdge )
  h= getHeight( picEdge )
  picNew= makeEmptyPicture( w, h )
  for x in range(0,w/2):
    for y in range (0,h/2):
      px=getPixel(picEdge,x,y)
      r=getRed(px)
      pxNew=getPixel(picNew,x,y)
      if r >0:
        setColor(pxNew,blue)
      else:
        setColor(pxNew,yellow)
  for x in range (w/2,w):
    for y in range (h/2,h):
      px=getPixel(picEdge,x,y)
      r=getRed(px)
      pxNew=getPixel(picNew,x,y)
      if r >0:
        setColor(pxNew,yellow)
      else:
        setColor(pxNew,blue)

  for x in range(0,w/2):
    for y in range (h/2,h):
      px=getPixel(picEdge,x,y)
      r=getRed(px)
      pxNew=getPixel(picNew,x,y)
      if r >0:
        setColor(pxNew,green)
      else:
        setColor(pxNew,red)
  for x in range (w/2,w):
    for y in range (0,h/2):
      px=getPixel(picEdge,x,y)
      r=getRed(px)
      pxNew=getPixel(picNew,x,y)
      if r >0:
        setColor(pxNew,red)
      else:
        setColor(pxNew,green)


  return picNew

沃霍尔化图片

您可以根据需要调整想要着色的象限。

I put the difference and intensity functions into the makeOutline function. I have called the warholize function from the makeOutline function.
Also, you needed to get the colors for the separate quadrants, here, I've just used getRed pixel to see if it is black or white (full color or not).

In this case I have used a threshold of 100 to optimise the effect. I have set the threshold within the makeOutline function, you can play with this, as you wish.

  def main():
  pic= makePicture( pickAFile() )
  show(pic)
  newPic= makeOutline(pic )
  show(newPic)

Pic

def makeOutline(pic ):
  picEdge=makeEmptyPicture(getWidth(pic),getHeight(pic))
  for x in range (0, getWidth(pic)-1):
    for y in range (0, getHeight(pic)-1):
      here=getPixel(picEdge,x,y)
      down = getPixel(pic,x,y+1)
      right = getPixel(pic, x+1,y)
      hereL=(getRed(here)+getGreen(here)+getBlue(here))/3
      downL=(getRed(down)+getGreen(down)+getBlue(down))/3
      rightL=(getRed(right)+getGreen(right)+getBlue(right))/3
      if abs (hereL-downL)>100 and abs(hereL-rightL)>100:
        setColor(here,black)
      if abs (hereL-downL)<=100 or abs(hereL-rightL)<=100:
        setColor(here,white)
  warholizedPic=warholize(picEdge)
  return warholizedPic

def warholize(picEdge):
  w= getWidth( picEdge )
  h= getHeight( picEdge )
  picNew= makeEmptyPicture( w, h )
  for x in range(0,w/2):
    for y in range (0,h/2):
      px=getPixel(picEdge,x,y)
      r=getRed(px)
      pxNew=getPixel(picNew,x,y)
      if r >0:
        setColor(pxNew,blue)
      else:
        setColor(pxNew,yellow)
  for x in range (w/2,w):
    for y in range (h/2,h):
      px=getPixel(picEdge,x,y)
      r=getRed(px)
      pxNew=getPixel(picNew,x,y)
      if r >0:
        setColor(pxNew,yellow)
      else:
        setColor(pxNew,blue)

  for x in range(0,w/2):
    for y in range (h/2,h):
      px=getPixel(picEdge,x,y)
      r=getRed(px)
      pxNew=getPixel(picNew,x,y)
      if r >0:
        setColor(pxNew,green)
      else:
        setColor(pxNew,red)
  for x in range (w/2,w):
    for y in range (0,h/2):
      px=getPixel(picEdge,x,y)
      r=getRed(px)
      pxNew=getPixel(picNew,x,y)
      if r >0:
        setColor(pxNew,red)
      else:
        setColor(pxNew,green)


  return picNew

Warholized pic

You can adjust which quadrants you want colored as you please.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文