A method for detecting a foreign object, performed by an electronic device including a screen and an image sensor, the method comprising: acquiring (101; 201; 301; 401) a detection image captured by the image sensor ; and detecting whether a foreign object is attached on the screen based on the detection image; wherein the detecting comprises: acquiring brightness of each pixel in the detection image; comparing brightness of a pixel in the detection image with brightness of each adjacent pixel of the pixel; characterized in the image sensor being arranged under a surface of the screen; marking the pixel as a target pixel in response to a brightness difference between the brightness of the pixel and the brightness of at least one adjacent pixel being greater than a threshold; obtaining a boundary between a bright region and a dark region in the detection image by connecting adjacent two target pixels, wherein brightness of the bright region is higher than brightness of the dark region; and determining a shape of the foreign object and/or a position of the foreign object on the screen based on a closed region enclosed by the boundary and/or one or more edges of the detection image where two endpoints of the boundary are located. acquiring (101; 201; 301; 401) a detection image captured by the image sensor ; and detecting whether a foreign object is attached on the screen based on the detection image; wherein the detecting comprises: acquiring brightness of each pixel in the detection image; comparing brightness of a pixel in the detection image with brightness of each adjacent pixel of the pixel; characterized in the image sensor being arranged under a surface of the screen; marking the pixel as a target pixel in response to a brightness difference between the brightness of the pixel and the brightness of at least one adjacent pixel being greater than a threshold; obtaining a boundary between a bright region and a dark region in the detection image by connecting adjacent two target pixels, wherein brightness of the bright region is higher than brightness of the dark region; and determining a shape of the foreign object and/or a position of the foreign object on the screen based on a closed region enclosed by the boundary and/or one or more edges of the detection image where two endpoints of the boundary are located. acquiring brightness of each pixel in the detection image; comparing brightness of a pixel in the detection image with brightness of each adjacent pixel of the pixel; characterized in the image sensor being arranged under a surface of the screen; marking the pixel as a target pixel in response to a brightness difference between the brightness of the pixel and the brightness of at least one adjacent pixel being greater than a threshold; obtaining a boundary between a bright region and a dark region in the detection image by connecting adjacent two target pixels, wherein brightness of the bright region is higher than brightness of the dark region; and determining a shape of the foreign object and/or a position of the foreign object on the screen based on a closed region enclosed by the boundary and/or one or more edges of the detection image where two endpoints of the boundary are located. <EOS>